您的位置:首页 > 其它

Codeforces Round #394 (Div. 2)D. Dasha and Very Difficult Problem【贪心】

2017-02-01 17:46 330 查看
D. Dasha and Very Difficult Problem

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Examples

Input
5 1 5
1 1 1 1 1
3 1 5 4 2


Output
3 1 5 4 2


Input
4 2 9
3 4 8 9
3 2 1 4


Output
2 2 2 9


Input
6 1 5
1 1 1 1 1 1
2 3 5 4 1 6


Output
-1


Note
Sequence b which was found in the second sample is suitable, because calculated sequence
c = [2 - 3, 2 - 4, 2 - 8, 9 - 9] = [ - 1,  - 2,  - 6, 0] (note that
ci = bi - ai) has compressed sequence equals to
p = [3, 2, 1, 4].

题目大意:

给你序列p和序列a,让你找一个合法的序列b,输出。

序列c是通过bi-ai来得到的。

序列p是通过离散化序列c来得到的。

思路:

1、既然序列p是通过序列c离散化得到的,那么其反应的就是一个从小到大的一个特征。那么首先我们按照p从小到大排序。

2、考虑ci=bi-ai.此时已知ai,求的是bi,那么bi=ci+ai;而ci是一个不定数组,其值只有大小顺序,而无固定值,那么我们要求的bi,只要合法,就是可行序列。

那么接下来我们贪心这个bi:

①设定p==1的位子上,bi=l,ci=l-ai;

②紧接着,设定p==2的位子上,尽可能的希望ci大于p==1的位子上的ci,并且p==2的位子上的ci尽可能的接近p==1的位子上的ci.

③依次类推,对于放置bi==r都不能满足ci>ci-1的情况下,那么就是-1的情况。

过程可能说的过于简单,具体参考代码(T T词穷了,不造咋说清楚些了)

Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int pos;
int num,val;
}a[100500];
int b[100500];
int ans[100500];
int c[100500];
int cmp(node a,node b)
{
return a.val<b.val;
}
int main()
{
int n,l,r;
while(~scanf("%d%d%d",&n,&l,&r))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].num);
a[i].pos=i;
}
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].val);
}
int flag=0;
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++)
{
if(i==1)
b[i]=l,c[i]=b[i]-a[i].num;
else
{
if(r-a[i].num>c[i-1])
{
b[i]=(c[i-1]+1+a[i].num);
if(b[i]<l)b[i]=l;
if(b[i]>r)flag=1;
c[i]=b[i]-a[i].num;
}
else flag=1;
}
ans[a[i].pos]=b[i];
}
if(flag==1)printf("-1\n");
else
{
for(int i=1;i<=n;i++)
{
printf("%d ",ans[i]);
}
printf("\n");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces#394Div. 2