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," ...
随机推荐
- 洗礼灵魂,修炼python(70)--爬虫篇—补充知识:json模块
在前面的某一篇中,说完了pickle,但我相信好多朋友都不懂到底有什么用,那么到了爬虫篇,它就大有用处了,而和pickle很相似的就是JSON模块 JSON 1.简介 1)JSON(JavaScrip ...
- Linux 安装golang
访问官方下载地址 或 https://studygolang.com/dl ,32位系统下载go1.9.4.linux-386.tar.gz,64位系统下载go1.9.4.linux-amd64.ta ...
- vue表单控件绑定(表单数据的自动收集)
v-model指令 你可以用 v-model 指令在表单控件元素上创建双向数据绑定.它会根据控件类型自动选取正确的方法来更新元素.尽管有些神奇 但 v-model 本质上不过是语法糖,它负责监听用户的 ...
- 使用 vagrant新建Linux虚拟机
准备工作 1.下载软件 2.安装软件 2.1 安装VirtualBox-5.1.34-121010-Win.exe 2.2 安装vagrant_2.0.3_x86_64.msi 3.新建 执行指令D: ...
- [tool] Visual Studio Code python配置
语言设置 安装中文插件即可成为中文 选择一个Python解释器 Python是一种解释型语言,为了运行Python代码并获取Python IntelliSense,您必须告诉VS Code使用哪个解释 ...
- 根据JavaBean创建数据库的操作SQL
根据JavaBean创建数据库的操作SQL import java.lang.reflect.Field; public class GenerateSQL { public static void ...
- P1182 数列分段`Section II`(贪心+二分, 好题)
这道题让我见识了二分的新姿势.本来,我是二分的位置的. 思路:直接二分答案x, 关键是检验函数的写法: 先用前缀和 a[i....], 看满足多少段满足 a[ j ]-a[ i ]<=x; 的注 ...
- 【移动端】移动端字体单位font-size选择px还是rem
对于只需要适配少部分手机设备,且分辨率对页面影响不大的,使用px即可对于需要适配各种移动设备,使用rem,例如只需要适配iphone和iPad等分辨率差别比较挺大的设备 html{font-size: ...
- Python代码分行问题
可以用“\”符号把一行过长的Python代码分解成几行,多个语句也可以写在同一行,语句之间要用“;”隔开.
- jenkins不能执行windows下的命令,cmd可执行。
1.TortoiseSVN程序在安装时候,选择在windows下的命令行执行,要安装 2.安装后,“C:\Program Files\TortoiseSVN\bin”路径下就存在了命令行的一些指令 3 ...