Codeforces 761D-Dasha and Very Difficult Problem

来源:互联网 发布:nodejs 数据库操作 编辑:程序博客网 时间:2024/06/03 21:47

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 nlr (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 51 1 1 1 13 1 5 4 2
output
3 1 5 4 2 
input
4 2 93 4 8 93 2 1 4
output
2 2 2 9 
input
6 1 51 1 1 1 1 12 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].


题意:数组a和数组b中每个元素都是不小于l,不大于r,数组c由数组b中元素减去数组a中对应位置的元素得到,即ci = bi - ai,数组p表示相应位置的数组c中的元素在数组c中排第几,现在告诉你l,r和数组a,数组p,若能得到数组b

则任写一组数组b,否则输出-1

解题思路:用结构体的形式来保存数组a,数组b,数组p,数组c中的各个元素,先以数组p来排序,则第一个b元素应

尽量小,所以可以定为l,后面的元素可以以此来推,若推出来的b元素小于l,则可以将其变大至l,若大于r,则说明

无法找到这样的数组b


#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;struct node{    int a,b,p,c;    int id;}x[100009];int n,l,r;bool cmp(node xx,node yy){    return xx.p<yy.p;}bool cmp1(node xx,node yy){    return xx.id<yy.id;}int main(){    while(~scanf("%d %d %d",&n,&l,&r))    {        for(int i=1;i<=n;i++)        {            scanf("%d",&x[i].a);            x[i].id=i;        }        for(int i=1;i<=n;i++)            scanf("%d",&x[i].p);        sort(x+1,x+1+n,cmp);        int flag=1;        for(int i=1;i<=n;i++)        {            if(i==1) x[i].b=l,x[i].c=l-x[i].a;            else            {                x[i].c=x[i-1].c+1;                x[i].b=x[i].a+x[i].c;                if(x[i].b<l) x[i].b=l,x[i].c=l-x[i].a;                if(x[i].b>r) flag=0;            }            if(flag==0) break;        }        if(!flag) printf("-1\n");        else        {            sort(x+1,x+1+n,cmp1);            printf("%d",x[1].b);            for(int i=2;i<=n;i++)                printf(" %d",x[i].b);            printf("\n");        }    }    return 0;}

0 0