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 ...
随机推荐
- 【杂题】[ARC070F] Honest Or Unkind【交互】
Description 这是一道交互题 有A+B个人,编号从0~A+B-1,其中有A个人是诚实的,B个人是居心叵测的. 你想知道每个人是诚实的还是居心叵测的. 询问可以用二元组(i,j)表示,代表问编 ...
- QTableWidgetItem QTreeWidgwtItem 复选框的取消显示方法
思路: QTableWidgetItem :item->setData(Qt::CheckStateRole,QVariant()); QTreeWidgwtItem :item-> ...
- [CSP-S模拟测试]:小L的数(数位DP+模拟)
题目传送门(内部题132) 输入格式 第一行一个整数$t$. 接下来$t$行每行一个整数$n$. 输出格式 $t$行,每行一个整数表示答案. 样例 样例输入: 41818231232691052109 ...
- HTMLHint 配置文件
HTMLHint 工具可以对 HTML 代码做静态代码检查,从而保证 HTML 代码的规范和质量.HTMLHint 工具内置 23 条规则,建议在 .htmlhintrc 配置文件中将规则尽可能都打开 ...
- WebView的基础用法
新建一个WebView项目,然后修改activity_main.xml布局文件中的代码,如下所示: <LinearLayout xmlns:android="http://schema ...
- C++入门经典-例2.9-输出十六进制数以及大写的十六进制数
1:代码如下: #include "stdafx.h" #include <iostream> #include <iomanip> using names ...
- forms authentication原理
细说ASP.NET Forms身份认证 asp.net 登陆验证 Form表单验证的3种方式 Understanding and Implementing ASP.NET Custom Forms A ...
- express node 框架介绍
开篇先声明一个重点: 就是论文件模块的重要性,之前我一直以为 fs 模块不重要,后来遇到了问题,才发现我之前的自以为是是多么愚蠢的一件事,我现在知道了 fs 模块的重要性 fs 模块:用于对文件的操作 ...
- Php mysql 常用代码、CURD操作以及简单查询
C/S:Client ServerB/S:Brower Server php主要实现B/S LAMP :Linux系统 A阿帕奇服务器 Mysql数据库 Php语言 mysql常用代码 ...
- java 设计模式 单例模式之饿汉模式/懒汉模式 singleton pattern
https://v.qq.com/x/page/e0364ung5zp.html 讲的不错, 关于 饿汉式单例模式 code Student 类: package com.test;//单例模式之 ...