【CodeForces - 761D 】Dasha and Very Difficult Problem (构造,思维)
題干:
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 2Output
3 1 5 4 2Input
4 2 9 3 4 8 9 3 2 1 4Output
2 2 2 9Input
6 1 5 1 1 1 1 1 1 2 3 5 4 1 6Output
-1Note
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].
?
解題報告:?
? ? 按照題意,先按照pos排序(我是從大到小排序,也可以從小到大),然后構造出最大的bi,顯然,讓bi==r是最好的(也就得到了c的最大值),這樣我們就找到了右邊界,然后一個一個讓c的那個值--,每次? --? 都 判斷一下算出的bi是否出邊界了(因為我們只要用c和a了,那就可以求出b)b=a+c,(ps:我好想是寫反了,寫成了b=a-c,所以一直讓tmp++了,其實應該是b=a+c? 然后 tmp--)。判斷左邊界,就是如果算出一個bi已經比 l 要小了,那么肯定就無解了,,(因為之前的每一步都是讓bi在合法狀態(tài)下取了最大值,結果到你這一步 算出來bi<l了,肯定是涼涼了),所以輸出-1就好了。
?
AC代碼:
#include<bits/stdc++.h> #define ll long long using namespace std; const int MAX = 1e5 + 5; ll n; ll l,r; ll a[MAX],pos[MAX],ans[MAX]; struct Node {ll pos,id; } node[MAX];bool cmp(Node a,Node b) {return a.pos > b.pos; } int main() {scanf("%lld",&n);scanf("%lld%lld",&l,&r);for(int i = 1; i<=n; i++) scanf("%lld",a+i);for(int i = 1; i<=n; i++) {scanf("%lld",&node[i].pos);node[i].id = i;} // if(n > r - l +1) { // printf("-1\n");return 0; // }sort(node+1,node+n+1,cmp);ll tmp = a[node[1].id]-r,flag=0;ans[node[1].id] = r;for(int i = 2; i<=n; i++) {tmp++;while(a[node[i].id] - tmp >r) tmp++;ans[node[i].id] = a[node[i].id] - tmp;if(a[node[i].id]-tmp<l) flag = 1;}if(flag == 1) {printf("-1\n");return 0;}printf("%lld",ans[1]);for(int i = 2; i<=n; i++) printf(" %lld",ans[i]);return 0 ; }總結:
? ?1WA,直接讓tmp--了,沒有讓他屬于合法范圍內,就是這一句while(a[node[i].id] - tmp >r) tmp++;。這里解釋一下為什么這個右邊界就可以,而左邊界就只要出界就輸出-1了。因為我們能找到合法解肯定要找合法解,我們出了右邊界,那就找一個剛好不出右邊界的唄。但是左邊界就不同了,因為在我們這種寫法下,tmp只能越來越大,才能滿足c離散化以后的那個p數(shù)組、、(因為我們這里tmp實際上是-c了)
? ?2WA,因為沒有看清條件,誤認為b是distinct的,但是其實是c是distinct的、、、所以加了
// if(n > r - l +1) { // printf("-1\n");return 0; // }這個判斷,,是不對的。
?
貼一個很簡潔的代碼:
注意掌握vis數(shù)組這樣的應用,就不需要開結構體了。?
總結
以上是生活随笔為你收集整理的【CodeForces - 761D 】Dasha and Very Difficult Problem (构造,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QuickBooks - QuickBo
- 下一篇: quickdcf.exe - quick