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. 【CF521C】Pluses everywhere(贡献)

    题意:有一个长为n的数字字符串,要求其中插入k个加号,求所有合法表达式的和之和 0<=k<n<=1e5 思路:参考官方题解,讲的很好很清楚 字符串下标从0开始 考虑第i位d[i]的贡 ...

  2. ReactJS 结合 antd-mobile 开发 h5 应用基本配置

    在 ReactJS 较为初级的使用 antd-mobile 使用时候直接加载 node_modules 文件中的相关 CSS,这个使用方法效率低:更高明的方法则按需加载,需要设置如下: 在 packa ...

  3. String Compression

    F. String Compression 利用dp和前缀数组来写 dp[i] 所表示的东西是 字符串 s[0:i] (不包括 s[i])能够压缩的最短长度 bj[i][j] 表示的是字符串 s[i: ...

  4. DVWA--Command Injection

    首先我们查看源代码一下 <?php if( isset( $_POST[ 'Submit' ] ) ) { // Get input $target = $_REQUEST[ 'ip' ]; / ...

  5. javascript 六种数据类型

    js的数据类型和常见隐式转化逻辑. 一.六种数据类型 原始类型(基本类型):按值访问,可以操作保存在变量中实际的值.原始类型汇总中null和undefined比较特殊. 引用类型:引用类型的值是保存在 ...

  6. GC类型以及不同类型GC的搭配 1

    jvm内存分配,以及gc算法在上两篇博客中已经有所介绍.接下来我们重点分析不同gc器的特点和他们的搭配使用(并非任何一种新生代GC策略都可以和另一种年老代GC策略进行配合工作)

  7. SSH上传文件出现error

    如上图,因为我在SSH上的用户没有对Linux服务器的文件夹的修改权限,我在linux上把文件夹的权限改为可读可改,就可以了

  8. 利用python的图像分块与拼接

    import os import matplotlib.pyplot as plt import cv2 import numpy as np def divide_img(img_path, img ...

  9. LeetCode 17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    题目描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出: ...

  10. 数组 Kotlin(5)

    数组 数组在 Kotlin 中使用 Array 类来表示,它定义了 get 和 set 函数(按照运算符重载约定这会转变为 [] ) 和 size 属性,以及一些其他有用的成员函数:基本类型 clas ...