C. Polycarp Restores Permutation
链接
[https://codeforces.com/contest/1141/problem/C]
题意
qi=pi+1−pi.给你qi让你恢复pi
每个pi都不一样
分析
就是数学吧
a1 +(a2-a1) +(a3-a1) +(a4-a1) +(a5-a1) +(a6-a1) +…+(an-a1)=a1+a2+....+an-(n-1)a1;
=n(n+1)/2-(n-1)*a1=a1+(a2-a1) +(a3-a1) +(a4-a1) +(a5-a1) +(a6-a1) +…+(an-a1)
(a2-a1) +(a3-a1) +(a4-a1) +(a5-a1) +(a6-a1) +…+(an-a1)可以用一个前缀和统计
a1=a1=( n × (n+1) / 2 -sum[n-1])/n;
在判断是否有重复的和超出范围的
看代码吧
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+10;
ll p[N],sum[N];
bool vis[N];
ll n;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("in.txt","r",stdin);
cout<<int('a')<<endl;
while(cin>>n){
sum[0]=0;
for(int i=1;i<n;i++)
{
cin>>p[i];
sum[i]=sum[i-1]+p[i];
}
for(int i=1;i<n;i++)
sum[i]+=sum[i-1];
ll a1=(n*(n+1)/2-sum[n-1])/n;
//cout<<a1<<endl;
if(a1<1||a1>n) cout<<-1<<endl;
else{
bool flag=0;
ll last=a1;
memset(vis,0,sizeof(vis));
vis[last]=1;
vector<ll> ve;
ve.push_back(last);
for(int i=2;i<=n;i++)
if(last+p[i-1]>=1&&last+p[i-1]<=n&&!vis[last+p[i-1]]){
last=last+p[i-1];
vis[last]=1;
ve.push_back(last);
}
else{
flag=1; break;
}
if(flag) cout<<-1;
else for(int i=0;i<n;i++) cout<<ve[i]<<' ';
cout<<endl;
}
}
return 0;
}
C. Polycarp Restores Permutation的更多相关文章
- CF 1141C Polycarp Restores Permutation
Description An array of integers p1,p2,…,pnp1,p2,…,pn is called a permutation if it contains each nu ...
- 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_{ ...
- Codeforces Round #547 (Div. 3) 题解
Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
随机推荐
- mssql sqlserver避免sql脚本中出现除零错误的方法分享
摘自:http://www.maomao365.com/?p=6612 摘要:下文介绍sql server中,sql脚本避免出现除零错误的方法分享 在各种业务系统开发中,通常会遇到除零的错误,下文分享 ...
- 转: OVER() 系列函数介绍
OVER(PARTITION BY)函数介绍 开窗函数 Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返 ...
- 解决git push时发现有超过100M的文件时,删除文件后,发现还是提交不上去的问题
我这里故意放了一个超过100M的文件 后续,git add ,git commit 然后,git push 此时会发现出现了错误.如果,我们再这里直接在文件系统中删除这个大的文件,然后再次提交,会发现 ...
- Kernel数据结构移植(list和rbtree)
主要移植了内核中的 list,rbtree.使得这2个数据结构在用户态程序中也能使用. 同时用 cpputest 对移植后的代码进行了测试.(测试代码其实也是使用这2个数据结构的方法) 内核代码的如下 ...
- June 6. 2018 Week 23rd Wednesday
You are confined only by the walls you build yourself. 限制你的只有你自己筑起的墙. From Andrew Murphy. Let's repe ...
- Should we ban guns 英语禁枪议论文
Should we ban guns ? 我们应该禁枪吗? 英语议论文 Should we ban guns? Author:Pleiades_Antares(www.cnblogs.com/iris ...
- TOPWAY智能彩色TFT液晶显示模块
TOPWAY 20年来专注工业显示, 以为全球工业用户提供稳定可靠和容易使用的液晶显示模块为己任, 智能彩色TFT液晶显示模块(以下简称智能模块), 就是我们在容易使用TFT 彩色液晶显示方向上为广大 ...
- 从此使用linux系统,但是QQ是必不可少的!!该篇文章方法成功!!!已验证!!!!!
一开始,我在Ubuntu14.04下安装的QQ版本是WineQQ2013SP6-20140102-Longene, 但后来发现这个版本QQ在linux下问题很多,比如不能用键盘输入密码,QQ表情使用失 ...
- nuxt框架Universal和Spa两种render mode的区别
如下图,官网上对于Universal 和 Spa 两种render mode的区别,并没有加以说明,相信大多数人跟我一样有点懵,不知道选什么好.虽然两个模式创建的项目看不出区别. 先给出推荐选项: U ...
- Java面试知识点之虚拟机篇(一)
前言:Java虚拟机的重要性不言而喻,不管是在实际工作中,还是面试中. 1.JVM架构 要点: 主要了解Java虚拟机运行时数据区:程序计数器.Java虚拟机栈.本地方法栈.Java堆和方法区. 参考 ...