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," ...
随机推荐
- 光盘yum源搭建
先修改基本的yum原源,使其不生效 cd /etc/yum.repos.d/ mv CentOS-Base.repo CentOS-Base.repo.bak 在修改媒介yum源使其生效 检验是否生效 ...
- Linux中shell和子shell一点点理解
Linux执行脚本有两种方式,主要区别在于是否建立子shell 1.像sh,bash,./命令是用来执行shell脚本的,在bash/sh命令下,脚本文件可以无"执行权限",即 ...
- python——函数之生成器
1 生成器函数的含义 生成器是一个返回可以迭代对象的函数,它是一个特殊的迭代器,但迭代器的抽象层级更高且比较复杂需要实现很多方法.相较迭代器而言,生成器简单使用. 2 生成器的创建方式 2.1 ...
- KVM使用
这里使用的是Ubuntu18.04桌面版虚拟机 关于KVM可以看一下我之前的博客,有一些简单的介绍. 1.在打开虚拟机之前先开启此虚拟机的虚拟化功能. 2.安装KVM及其依赖项 wy@wy-virtu ...
- Android Studio教程03-Activtiy生命周期的理解
目录 1. Activity 1.1. 安卓中的Activity定义和特性: 1.2. 注册Activity 1. Intent filters:设置默认开启的activity 1.3. Activi ...
- VS code常用的几个插件
VScode是一个我最近才开始用的编辑器,在此列几个自己在用的插件,以备换机时的需要. auto close tagbeautify css/sass/scss/lessone dark themes ...
- 【HNOI2018】排列
[HNOI2018]排列 神仙贪心题. 题目说这么大堆东西就是想告诉你这是个森林,选了\(v\)的父亲后才能选\(v\). 我们设\(w_v\)为\(v\)所在联通块权值和,\(size_v\)表示\ ...
- linux stat 查看文件的详细信息
linux stat查看文件详细信息 [root@oldboy01 tmp]# stat /tmp/1.cc File: `/tmp/1.cc' Size: 4 Blocks: 8 IO Block: ...
- 正确生成浮点型的方法,解决sqlachemy Float浮点型的坑,生成float类型时,长度和精度均为0,导致查询不到结果!
问题描述 在使用flask_sqlachemy时,给price字段选择了Float类型,数据库用的mysql,生成数据库表后,发现 from sqlalchemy import Float,Colum ...
- js同步-异步-回调
出处:https://blog.csdn.net/u010297791/article/details/71158212(1)上面主要讲了同步和回调执行顺序的问题,接着我就举一个包含同步.异步.回调的 ...