$CF1141C Polycarp Restores Permutation$
\(problem\)
这题的大致意思就是已知数值差值 求1-n的排列
如果能构成排列 则输出这个排列。如果不能则输出-1
排列的值都是 大于1 而小于n的 而且没有相同的数字。
这题最关键的是 怎么输出这个序列 有的是存在负数的。
那么 考虑一下排列都是从1到n的对不对。
取序列的最小值 然后用\(1 - Min\)即是整个序列应该加上的数值。
首先考虑判重。 用数组 或者用\(Map\) \(or\) \(Set\) 。
都是不错的方法。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
inline LL In() { LL res(0),f(1); register char c ;
while(isspace(c=getchar())) ; c == '-'? f = -1 , c = getchar() : f = 1 ;
while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(c=getchar())) ; return res * f ;
}
int n ;
int p[200000+5] ;
int res[200000+5] ;
set<int> s;//判重
signed main(){
n = In() ;
for(register int i=1;i<=n-1;i++) p[i] = In() ;
res[1] = 0 ;
for(register int i=2;i<=n;i++) res[i] = res[i-1] + p[i-1] ;//计算前缀和
int Min = 0x7f7f7f7f;
for(register int i=1;i<=n;i++){
Min = min(res[i],Min);
if(s.count(res[i])==0) s.insert(res[i]) ;//插入数值
else {
cout << -1 << endl ;//如果存在重复的数值则不可能构成排列。
return 0 ;
}
}
//cout << Min << endl ;
int c = 1 - Min ;
bool f = false;
for(register int i=1;i<=n;i++) {
if(res[i] + c > n) {//如果大于n则不能构成 也是输出-1
cout << -1 << endl ;
return 0 ;
}
}
for(register int i=1;i<=n;i++) {
cout << res[i] + c << ' ' ;
}
return 0 ;
}
随机推荐
- 爬虫实战(三) 用Python爬取拉勾网
目录 0.前言 1.初始化 2.爬取数据 3.保存数据 4.数据可视化 5.大功告成 0.前言 最近,博主面临着选方向的困难(唉,选择困难症患者 >﹏<),所以希望了解一下目前不同岗位的就 ...
- 基于服务器版centos7的Hadoop/spark搭建
前提说明: 1.Hadoop与spark是两个独立的框架,只安装spark也可独立运行,spark有自己的调度器(standalone模式): 2.在Hadoop的基础上安装spark就是为了使用ya ...
- Python习题之列表排序,4种方法
def sort_list_method_1(a): return sorted(a) print(sort_list_method_1([1, 4, 2])) def sort_list_metho ...
- Jmeter使用基础笔记-认识Jmeter
我在工作过程中接触Jmeter不算特别多,对Jmeter的使用也只是限于基础阶段,不过对付基本的一些需求我想足够使用了.有好几个朋友问我关于Jmeter的问题,在此我将我在工作过程中的使用心得和总结的 ...
- The Falling Leaves(建树方法)
uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of ...
- IOC容器Autofac使用
今天我沉浸在IOC的设计模式追求中,听了很多课,也看了很多例子,这个是我的一个测试项目 为了测试代码,我首先准备了两个类一个Car和接口ICar,这两个类和我们平时项目中的DAL与IDAL相似,现在我 ...
- @requestbody---接受前端传json对象并绑定javabean
@requestbody---接受前端传json对象并绑定javabean----https://blog.csdn.net/ljxbbss/article/details/74452326 最近代码 ...
- Ubuntu 16.04设置开机启动应用程序
在终端通过以下命令进行设置,Dash已经搜索不到Startup了: gnome-session-properties 或者直接在Dash中搜索:gnome-session
- Nginx 重写规则指南
作者:运维生存时间 - 默北 链接:www.ttlsa.com/nginx/nginx-rewriting-rules-guide/ 当运维遇到要重写情况时,往往是要程序员把重写规则写好后,发给你,你 ...
- MySQL: How to support full Unicode in MySQL databases
How to support full Unicode in MySQL databases Published 30th July 2012 · tagged with MySQL, securit ...