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.
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.
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.
5 1 5 1 1 1 1 1 3 1 5 4 2
3 1 5 4 2
4 2 9 3 4 8 9 3 2 1 4
2 2 2 9
6 1 5 1 1 1 1 1 1 2 3 5 4 1 6
-1
给一个数列a,给一个数列p,已知数列p是数列c按个元素大小编号1~n后的所谓的压缩数列,求任意一个符合条件的数列b=a+c(条件:数列a、b都在某个范围内);
思路:
例如对于第二组样例,
3 4 8 9
根据 3 2 1 4的大小重排后得到
8 4 3 9 (a)
1 2 3 4 (p)
相对应的,不妨假设数列b对应的第一位为下界2,那么可知数列c的对应第一位为-6
8 4 3 9 (a)
1 2 3 4 (p)
-6 (c)
2 (b)
再看第二位,不妨假设b还是下界,则c为-2,-2 > -6,符合p给定的大小顺序,则可以有:
8 4 3 9 (a)
1 2 3 4 (p)
-6 -2 (c)
2 2 (b)
再到第三位,不妨设b还是下界,则c为-1,依然符合p的大小顺序
8 4 3 9 (a)
1 2 3 4 (p)
-6 -2 -1 (c)
2 2 2 (b)
再到第四位,设b为下界,则c为-7,这时候发现不符合顺序,那么就让c等于前一位+1
8 4 3 9 (a)
1 2 3 4 (p)
-6 -2 -1 0 (c)
2 2 2 9 (b)
这时候,得到的b为9,不大于上界,符合条件,因此我们得到了一个符合条件的b:
2 2 2 9 (b)
最后我们在将数列b排回3 2 1 4的顺序即可(这步很重要……在这个样例里重新排序之后没有变化,但其他样例就不一定了……)
3 2 1 4 (p)
2 2 2 9 (b)
感觉自己写的代码一点都不优雅……(羞耻……
1 #include<cstdio> 2 int main() 3 { 4 int n,l,r,a[100000+5],temp_a[100000+5],p[100000+5],c[100000+5]; 5 scanf("%d %d %d",&n,&l,&r); 6 for(int i=1;i<=n;i++) scanf("%d",&temp_a[i]); 7 for(int i=1;i<=n;i++) scanf("%d",&p[i]); 8 for(int i=1;i<=n;i++) a[ p[i] ]=temp_a[i]; 9 10 //for(int i=1;i<=n;i++) printf("%d ",a[i]);printf(" "); 11 12 int now=l-a[1]-1; 13 for(int i=1;i<=n;i++) 14 { 15 if(l-a[i] > now) c[i]=(now=l-a[i]); 16 else c[i]=(now+=1); 17 if( c[i]+a[i] > r ){ 18 printf("-1 "); 19 return 0; 20 } 21 } 22 23 for(int i=1;i<=n;i++){ 24 if(i!=1) printf(" "); 25 printf("%d",c[p[i]]+a[p[i]]); 26 } 27 printf(" "); 28 return 0; 29 }