Codeforces Round #547 (Div. 3)C. Polycarp Restores Permutation
C. Polycarp Restores Permutation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
题意:输入一个数n表示一个序列长度,接下来输入n-1个数表示该序列的差分数组构成
要求还原成一个1-n构成的序列,无解输出-1
思路:一个很明显的解法就是我先假定第一个数为最大数n,然后得到一个序列,判断其是否连续是则还原(按照对应差值还原),否则无解
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 200005
const double eps = 1e-8;
using namespace std;
inline ll read()
{
char ch = getchar(); ll s = 0, w = 1;
while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
return s * w;
}
inline void write(ll x)
{
if (x < 0)putchar('-'), x = -x;
if (x > 9)write(x / 10);
putchar(x % 10 + 48);
}
ll n,a[10*maxn+5],minn=inf,flag[20*maxn],maxx=-1,b[maxn],c[maxn];
inline bool check()
{
ll i=1,cnt=0;
while(b[i+1]-b[i]==1)
{
// cout<<b[i]<<endl;
i++,cnt++;
}
return cnt+1==n?1:0;
}
int main()
{
n=read();
a[1]=n,flag[n]=1,minn=min(a[1],minn),maxx=max(maxx,a[1]);
b[1]=a[1];
for(rg i=2;i<=n;i++)
{
ll x=read();
a[i]=a[i-1]+x,b[i]=a[i];
//p.push_back(a[i]);
minn=min(minn,a[i]);
maxx=max(maxx,a[i]);
}
sort(b+1,b+1+n);
/*for(rg i=1;i<=n;i++)
{
cout<<b[i]<<endl;
}*/
if(check())
{
ll k=maxx-n;
for(rg i=1;i<=n;i++)
{
a[i]-=k;
// flag[a[i]]=1;
// i==n?cout<<a[i]<<endl:cout<<a[i]<<" ";
}
for(rg i=1;i<=n;i++)
{
i==n?cout<<a[i]<<endl:cout<<a[i]<<" ";
}
}
else
{
cout<<-1<<endl;
return 0;
}
return 0;
}
- windows下nvm安装node之后npm命令找不到问题解决办法
- 元素加了position:absolute则该元素的text-align:center居中失效的解决办法
- Chrome设置断点的各种姿势
- React-native踩坑小记
- 带修改莫队算法
- P2590 [ZJOI2008]树的统计
- #103. 子串查找
- P2051 [AHOI2009]中国象棋
- P2216 [HAOI2007]理想的正方形(二维RMQ)
- P3376 【模板】网络最大流(70)
- android客服端+eps8266+单片机+路由器之远程控制系统
- P2740 [USACO4.2]草地排水Drainage Ditches
- P1343 地震逃生
- html5 移动适配写法
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法