CF 1141C Polycarp Restores Permutation
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的更多相关文章
- C. Polycarp Restores Permutation
链接 [https://codeforces.com/contest/1141/problem/C] 题意 qi=pi+1−pi.给你qi让你恢复pi 每个pi都不一样 分析 就是数学吧 a1 +(a ...
- Polycarp Restores Permutation
http://codeforces.com/contest/1141/problem/C一开始没想法暴力的,next_permutation(),TLE 后来看了这篇https://blog.csdn ...
- $CF1141C Polycarp Restores Permutation$
\(problem\) 这题的大致意思就是已知数值差值 求1-n的排列 如果能构成排列 则输出这个排列.如果不能则输出-1 排列的值都是 大于1 而小于n的 而且没有相同的数字. 这题最关键的是 怎么 ...
- Codeforces Round #547 (Div. 3) C. Polycarp Restores Permutation (数学)
题意:有一长度为\(n\)的序列\(p\),现在给你\(q_i=p_{i+1}-q_i \ (1\le i\le n)\),问你是否能还原出原序列,如果能救输出原序列,否则输出\(-1\). 题解:由 ...
- CF1141C Polycarp Restores Permutation 题解
Content 给定一个长度为 \(n-1\) 的序列 \(q\),问你是否能找到一个 \(1\sim n\) 的排列 \(p\),使得 \(\forall i\in[1,n)\),\(q_i=p_{ ...
- CF 1006B Polycarp's Practice【贪心】
Polycarp is practicing his problem solving skill. He has a list of n problems with difficulties a1,a ...
- cf B. Levko and Permutation
http://codeforces.com/contest/361/problem/B #include <cstdio> #include <cstring> #includ ...
- CF 500B New Year Permutation
传送门 题目大意 给你一个数列,再给你一个矩阵,矩阵的(i,j)如果为1就表示可以将i,j位置上的数交换,问任意交换之后使原数列字典序最小并输出. 解题思路 因为如果i与j能交换,j与k能交换,那么i ...
- Codeforces Round #547 (Div. 3) 题解
Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...
随机推荐
- PHP教程-反序列化的方法
序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性.兄弟连PHP培训() 1. ...
- idea2018.3.6安装与破解教程(亲测可用、破解到2100年)
最近,帮室友进行idea安装,之前自己安装借鉴的博客已404,在网上找了好几个都无效,想着总结一份备用. 此博客是又找了一台电脑,边安装边写的. 目录 (已安装好的,可以直接看idea2018.3.6 ...
- Windows安装MongoDB .zip绿色版
本文链接:https://blog.csdn.net/HTouying/article/details/88428452 MongoDB官网下载链接:https://www.mongodb.com/d ...
- CentOS7 服务器上如何安装python3
1.官网下载python3的源码包 网址:https://www.python.org/ 进去之后点击导航栏的Downloads,也可以鼠标放到Downloads上弹出菜单选择Source code, ...
- tomcat 散点杂记
tomcat有很多细碎的知识点和一些坑点,我将再次记录 域名直指项目 我们经常访问项目都要带上项目目录 eg: http://xwiki.test.com/xwiki or http://jenkin ...
- CodeForces 788B--Weird journey
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Little ...
- Max Sum Plus Plus(最大m字段和,优化)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description Now I t ...
- 创建Idea创建SpringBoot项目 - 各个目录的解释
[SpringBoot-创建项目]一.通过Idea创建SpringBoot项目 一.首先我们通过Idea创建一个新项目 二.选择sdk和快速构建模板 三.填写项目基本信息 三.选择项目依赖 四.填写项 ...
- 查看线程的cpu占用率
1) top -H -p 进程pid 查看线程的线程ID与CPU占用情况.或者使用 ps -eLo pid,lwp,pcpu | grep 进程pid2) pstack ...
- java 设计模式 单例模式之饿汉模式/懒汉模式 singleton pattern
https://v.qq.com/x/page/e0364ung5zp.html 讲的不错, 关于 饿汉式单例模式 code Student 类: package com.test;//单例模式之 ...