D. Dasha and Very Difficult Problem
题目连接:
http://codeforces.com/contest/761/problem/D
Description
Dasha logged into the system and began to solve problems. One of them is as follows:
Given two sequences a and b of length n each you need to write a sequence c of length n, the i-th element of which is calculated as follows: ci = bi - ai.
About sequences a and b we know that their elements are in the range from l to r. More formally, elements satisfy the following conditions: l ≤ ai ≤ r and l ≤ bi ≤ r. About sequence c we know that all its elements are distinct.
Dasha wrote a solution to that problem quickly, but checking her work on the standard test was not so easy. Due to an error in the test system only the sequence a and the compressed sequence of the sequence c were known from that test.
Let’s give the definition to a compressed sequence. A compressed sequence of sequence c of length n is a sequence p of length n, so that pi equals to the number of integers which are less than or equal to ci in the sequence c. For example, for the sequence c = [250, 200, 300, 100, 50] the compressed sequence will be p = [4, 3, 5, 2, 1]. Pay attention that in c all integers are distinct. Consequently, the compressed sequence contains all integers from 1 to n inclusively.
Help Dasha to find any sequence b for which the calculated compressed sequence of sequence c is correct.
Input
The first line contains three integers n, l, r (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ 109) — the length of the sequence and boundaries of the segment where the elements of sequences a and b are.
The next line contains n integers a1, a2, …, an (l ≤ ai ≤ r) — the elements of the sequence a.
The next line contains n distinct integers p1, p2, …, pn (1 ≤ pi ≤ n) — the compressed sequence of the sequence c.
Output
If there is no the suitable sequence b, then in the only line print “-1”.
Otherwise, in the only line print n integers — the elements of any suitable sequence b.
Sample Input
5 1 5
1 1 1 1 1
3 1 5 4 2
Sample Output
3 1 5 4 2
Hint
题意
c[i]=b[i]-a[i],现在给你a[i]和c[i]的相对大小,问你可不可能出现b[i]满足条件,如果有的话,输出。
题解:
贪心,最小的显然要最小,次小的在比最小大的基础上最小就好了。
对于每一个数都二分一下就完了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
long long l,r,a[maxn],b[maxn],c[maxn];
priority_queue<pair<int,int> >S;
int n,op[maxn];
int main()
{
scanf("%d%lld%lld",&n,&l,&r);
for(int i=0;i<n;i++){
scanf("%lld",&a[i]);
}
for(int i=0;i<n;i++)
scanf("%d",&op[i]),S.push(make_pair(op[i],i));
long long last = -1e16;
while(!S.empty()){
long long now = S.top().second;
S.pop();
long long L=l,R=r,Ans=r+1;
while(L<=R){
long long mid=(L+R)/2;
if(a[now]-mid>last){
L=mid+1,Ans=mid;
}else
R=mid-1;
}
if(Ans==r+1){
cout<<"-1"<<endl;
return 0;
}
b[now]=Ans;
last=a[now]-Ans;
}
for(int i=0;i<n;i++)
cout<<b[i]<<" ";
cout<<endl;
}