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 ...
随机推荐
- Spring 动态多数据源
spring springmvc mybatis 多数据源配置时的重点: 1. 注意事务拦截器的配置 Spring中的事务管理与数据源是绑定的,一旦程序执行到Service层(事务管理)的话,由于在进 ...
- BZOJ 3551: [ONTAK2010]Peaks加强版 Kruskal重构树+dfs序+主席树+倍增
建出来 $Kruskal$ 重构树. 将询问点向上跳到深度最小,且合法的节点上. 那么,得益于重构树优美的性质,这个最终跳到的点为根的所有子节点都可以与询问点互达. 对于子树中求点权第 $k$ 大的问 ...
- 【PKUSC2019】线弦图【计数】【树形DP】【分治FFT】
Description 定义线图为把无向图的边变成点,新图中点与点之间右边当且仅当它们对应的边在原图中有公共点,这样得到的图. 定义弦图为不存在一个长度大于3的纯环,纯环的定义是在环上任取两个不相邻的 ...
- phpfor函数和foreach函数
PHP for 循环 PHP While 循环 PHP 函数 PHP for 循环执行代码块指定的次数. PHP for 循环 如果您已经提前确定脚本运行的次数,可以使用 for 循环. 语法 for ...
- 正则表达式中常用的模式修正符有i、g、m、s、x、e详解
正则表达式中常用的模式修正符有i.g.m.s.x.e等.它们之间可以组合搭配使用. 它们的作用如下: //修正符:i 不区分大小写的匹配; //如:"/abc/i"可以与abc或a ...
- 通过实例聊聊Java中的多态
Java中的多态允许父类指针指向子类实例.如:Father obj=new Child(); 那么不禁要发问?? 使用这个父类型的指针访问类的属性或方法时,如果父类和子类都有这个名称的属性或方法,哪 ...
- 如何在form组件中添加一个单选或者多选的字段
解决办法: 需要在增加的类里面加入choices 具体操作如下:
- BOSCH汽车工程手册————混合驱动
首先放一波资源,一千两百多页的pdf 链接:https://pan.baidu.com/s/15IsvHqOFCnqAKwY_SR4-lA提取码:6wmz 混合驱动 混合驱动有串联驱动并联驱动以及两种 ...
- tf工程化部署相关
1.TensorFlow 模型保存/载入的两种方法 https://blog.csdn.net/thriving_fcl/article/details/71423039 [讲解清晰,2种方法都有缺陷 ...
- 下载的管理类MyDownloadManager
import android.content.Intent; import android.net.Uri; import java.io.File; import java.io.FileOutpu ...