前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #547 (Div. 3)C. Polycarp Restores Permutation

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

作者头像
glm233
发布2020-09-28 10:34:03
4570
发布2020-09-28 10:34:03
举报

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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档