An array of integers p1,p2,…,pnp1,p2,…,pn is called a permutation if it contains each number from 11 to nn exactly once. For example, the following arrays are permutations: [3,1,2][3,1,2], [1][1], [1,2,3,4,5][1,2,3,4,5] and [4,3,1,2][4,3,1,2]. The following arrays are not permutations: [2][2], [1,1][1,1], [2,3,4][2,3,4].
Polycarp invented a really cool permutation p1,p2,…,pnp1,p2,…,pn of length nn. It is very disappointing, but he forgot this permutation. He only remembers the array q1,q2,…,qn−1q1,q2,…,qn−1 of length n−1n−1, where qi=pi+1−piqi=pi+1−pi.
Given nn and q=q1,q2,…,qn−1q=q1,q2,…,qn−1, help Polycarp restore the invented permutation.
Input
The first line contains the integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the length of the permutation to restore. The second line contains n−1n−1 integers q1,q2,…,qn−1q1,q2,…,qn−1 (−n<qi<n−n<qi<n).
Output
Print the integer -1 if there is no such permutation of length nn which corresponds to the given array qq. Otherwise, if it exists, print p1,p2,…,pnp1,p2,…,pn. Print any such permutation if there are many of them.
3
-2 1
3 1 2
5
1 1 1 1
1 2 3 4 5
4
-1 2 2
-1
之前想求出q1,后来一直wa加re。。。(从37到42),后来换了一种思路,用前缀求和(也re,到47),就在网上看到可以用map。果然ac了。。。最后学长帮我看了一下,交换了一下一个地方就ac了。。。以后细心一点,判断的地方应该先进行哪些判断
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<cstring>
#include<string>
#include<math.h>
using namespace std;
typedef long long ll;
int main()
{
ll n,q,flag=1,x[200005];
ll minn=0;
bool y[200005];
x[0]=0;
memset(y,false,sizeof(y));
scanf("%lld",&n);
for(int i=1;i<n;i++)
{
scanf("%lld",&q);
x[i]=x[i-1]+q;
minn=min(minn,x[i]);
}
for(int i=0;i<n;i++)
{
if(x[i]-minn+1>n||y[x[i]-minn+1]==true)
{
flag=0;
break;
}
y[x[i]-minn+1]=true;
}
if(flag==0) printf("-1\n");
else
{
for(int i=0;i<n-1;i++) printf("%lld ",x[i]-minn+1);
printf("%lld\n",x[n-1]-minn+1);
}
return 0;
}