Codeforces Round #547 (Div. 3)C. Polycarp Restores Permutation

2020-09-28 10:34:03 浏览数 (1)

C. Polycarp Restores Permutation

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

题意:输入一个数n表示一个序列长度,接下来输入n-1个数表示该序列的差分数组构成

要求还原成一个1-n构成的序列,无解输出-1

思路:一个很明显的解法就是我先假定第一个数为最大数n,然后得到一个序列,判断其是否连续是则还原(按照对应差值还原),否则无解

代码语言:javascript复制
// luogu-judger-enable-o2
#include<bits/stdc  .h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 200005
const double eps = 1e-8;
using namespace std;
inline ll read()
{
	char ch = getchar(); ll s = 0, w = 1;
	while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
	while (ch >= 48 && ch <= 57) { s = (s << 1)   (s << 3)   (ch ^ 48); ch = getchar(); }
	return s * w;
}
inline void write(ll x)
{
	if (x < 0)putchar('-'), x = -x;
	if (x > 9)write(x / 10);
	putchar(x % 10   48);
}
ll n,a[10*maxn 5],minn=inf,flag[20*maxn],maxx=-1,b[maxn],c[maxn];
inline bool check()
{
    ll i=1,cnt=0;
    while(b[i 1]-b[i]==1)
    {
      //  cout<<b[i]<<endl;
        i  ,cnt  ;
 
    }
    return cnt 1==n?1:0;
}
int main()
{
	n=read();
	a[1]=n,flag[n]=1,minn=min(a[1],minn),maxx=max(maxx,a[1]);
	b[1]=a[1];
    for(rg i=2;i<=n;i  )
    {
        ll x=read();
        a[i]=a[i-1] x,b[i]=a[i];
        //p.push_back(a[i]);
        minn=min(minn,a[i]);
        maxx=max(maxx,a[i]);
    }
   sort(b 1,b 1 n);
   /*for(rg i=1;i<=n;i  )
   {
       cout<<b[i]<<endl;
   }*/
    if(check())
    {
        ll k=maxx-n;
        for(rg i=1;i<=n;i  )
        {
            a[i]-=k;
           // flag[a[i]]=1;
          //  i==n?cout<<a[i]<<endl:cout<<a[i]<<" ";
        }
            for(rg i=1;i<=n;i  )
        {
            i==n?cout<<a[i]<<endl:cout<<a[i]<<" ";
        }
    }
    else
    {
        cout<<-1<<endl;
        return 0;
    }
	return 0;
}

0 人点赞