Description

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.

Examples

Input

3 -2 1

Output

3 1 2

Input

5 1 1 1 1

Output

1 2 3 4 5

Input

4 -1 2 2

Output

-1

题目分析

给你一个数组的相邻两项之间的差值,要求还原这个数组,而且这个数组中的元素的取值范围在[1,n],并且每一个数只出现一次,并且1-n的每一个数都要出现.

想到这里,觉得这个题也不难呀,二分枚举第一个数然后根据前后的差值不断的向后递推就好了,不过注意判重。

如果第一个数太大了,那么必然会出现数组中的某一个数越界,那么就继续二分左区间,否则就二分右区间。

而在某一个数作为第一个数的时候出现了某一个数重复出现的情况,则说明这个数组无法还原.

代码区

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int Max = 5e5 + 5; int v[Max];
; int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
for (int i = 1; i < n; i++)
{
scanf("%d", v + i);
}
int l, r;
if (v[1] > 0)l = 1, r = n - v[0]; //若前两个数的差为正数,那么第一个数的小于n-v[0],因为第二个数不可以超过n
else l = 1 - v[0], r = n; //若前两个数的差为负数,那么第一个数一定要大于1+v[0]
vector<int>q; //存数据
bool vis[Max]; //vis[i]记录数字i是否出现 int flag = false, gg = false; //flag == true代表成功,gg = true代表失败
while (l <= r) //二分
{
int mid = (l + r) / 2;
memset(vis, false, sizeof(vis));
vis[mid] = true; //表示mid出现
q.push_back(mid); int next; //记录序列下一个数
for (int i = 1; i < n;i++)
{
next = q.back() + v[i]; //记录下一个数
if (1 <= next && next <= n && !vis[next])//当前数不重复,且在范围[1,n]内
{
q.push_back(next);
vis[next] = true;
if (q.size() == n) flag = true;
}
else
{
if (1 <= next && next <= n && vis[next])gg = true; //出现重复,代表这个序列不可能成了
break;
}
}
if (gg || flag) break; //到达目的或者无法实现
q.clear(); //当前作为一个数的数失败,则继续二分
if (next > n)r = mid - 1; //第一个数太小导致越界
else l = mid + 1; //第一个数太大导致越界
}
if (gg)
{
printf("-1\n");
}
else
{
if(flag)
{
for (int i = 0;i < n - 1; i++)
{
printf("%d ", q[i]);
}
printf("%d\n", q[n - 1]);
}
else
{
printf("-1\n");
}
}
}
return 0;
}

CF 1141C Polycarp Restores Permutation的更多相关文章

  1. C. Polycarp Restores Permutation

    链接 [https://codeforces.com/contest/1141/problem/C] 题意 qi=pi+1−pi.给你qi让你恢复pi 每个pi都不一样 分析 就是数学吧 a1 +(a ...

  2. Polycarp Restores Permutation

    http://codeforces.com/contest/1141/problem/C一开始没想法暴力的,next_permutation(),TLE 后来看了这篇https://blog.csdn ...

  3. $CF1141C Polycarp Restores Permutation$

    \(problem\) 这题的大致意思就是已知数值差值 求1-n的排列 如果能构成排列 则输出这个排列.如果不能则输出-1 排列的值都是 大于1 而小于n的 而且没有相同的数字. 这题最关键的是 怎么 ...

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

    题意:有一长度为\(n\)的序列\(p\),现在给你\(q_i=p_{i+1}-q_i \ (1\le i\le n)\),问你是否能还原出原序列,如果能救输出原序列,否则输出\(-1\). 题解:由 ...

  5. CF1141C Polycarp Restores Permutation 题解

    Content 给定一个长度为 \(n-1\) 的序列 \(q\),问你是否能找到一个 \(1\sim n\) 的排列 \(p\),使得 \(\forall i\in[1,n)\),\(q_i=p_{ ...

  6. CF 1006B Polycarp's Practice【贪心】

    Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a1,a ...

  7. cf B. Levko and Permutation

    http://codeforces.com/contest/361/problem/B #include <cstdio> #include <cstring> #includ ...

  8. CF 500B New Year Permutation

    传送门 题目大意 给你一个数列,再给你一个矩阵,矩阵的(i,j)如果为1就表示可以将i,j位置上的数交换,问任意交换之后使原数列字典序最小并输出. 解题思路 因为如果i与j能交换,j与k能交换,那么i ...

  9. Codeforces Round #547 (Div. 3) 题解

    Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...

随机推荐

  1. sublime text怎么格式化PHP代码

    手动安装: 可能由于各种原因,无法使用代码安装,那可以通过以下步骤手动安装Package Control: 1.点击Preferences > Browse Packages菜单 2.进入打开的 ...

  2. try捕获SQL异常

  3. ASP.NET上传断点续传

    IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag 客户端每次提交下载请求时,服务 ...

  4. CPU风扇转速异常

    本文适用于Ubuntu 16.04,造冰箱的大熊猫@cnblogs 2018/10/9 近日发现一个问题,新笔记本的CPU风扇转速很高.笔记本刚刚启动,就能听到风扇呼呼的声音,转速高的异常.以前不是这 ...

  5. Linux扩展swap分区

    一.将sda磁盘进行分区: 先查看sda磁盘已经使用了多少主分区,如下图所示,主分区已使用3个,所以应选择扩展分区: 二.再将扩展分区进行分区: 三.分区完成后执行partprobe使系统重新识别分区 ...

  6. 捕获有问题的SQL

  7. html密码框value为空,但是总有默认密码(原)

    input输入框加属性:autocomplete="new-password" ,浏览器就不会给他填充默认密码. <input class="form-contro ...

  8. R_Studio中对xls文件学生总成绩统计求和

    我们发现这张xls表格是没有学生总分的,在xls文件中计算学生总分嫌麻烦时,可以考虑在R Studio中自定义R Script脚本来解决实际问题(计算每个学生的总成绩) .xls数据表中的数据(关键信 ...

  9. python语言中多继承中super调用所有父类的方法以及要用到的MRO顺序

    在python多继承中,利用super().父类方法,可以调用所有父类,从而在重写的状态下,再次对所有父类的调用! 例: print("******多继承使用super().__init__ ...

  10. (转)php中字符过滤

    有时候为了安全起见,我们需要对用户输入的字符串进行转义       文章中有不正确的或者说辞不清的地方,麻烦大家指出了--- 与PHP字符串转义相关的配置和函数如下: 1.magic_quotes_r ...