• 题意:有一长度为\(n\)的序列\(p\),现在给你\(q_i=p_{i+1}-q_i \ (1\le i\le n)\),问你是否能还原出原序列,如果能救输出原序列,否则输出\(-1\).

  • 题解:由:\(q_i=p_{i+1}-p_i\),我们对其求前缀和可得:\(s_i=p_{i+1}-p_1\),然后再求出:\(\sum^{n-1}_{i=1}s_i=\sum^{n}_{i=2}p_i-(n-1)*p_1\),\(\sum^{n}_{i=2}p_i=\sum^{n}_{i=1}i-p_1=\frac{(n)*(n+1)}{2}-p_1\),所以:\(\sum^{n-1}_{i=1}s_i= \frac{(n)*(n+1)}{2}-n*p_1\),直接推出\(p_1\),然后线性求出所有\(p_i\)再判断一下就好了.

  • 代码:

    ll n;
    ll q[N],p[N];
    ll c[N];
    map<ll,int> mp; int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n;
    rep(i,1,n-1) cin>>q[i]; rep(i,1,n-1) c[i]=c[i-1]+q[i]; ll cnt=0;
    rep(i,1,n-1) cnt+=c[i];
    ll sum=n*(n+1)/2;
    sum-=cnt;
    if(sum%n!=0){
    cout<<-1<<'\n';
    return 0;
    }
    p[1]=sum/n; rep(i,1,n-1){
    p[i+1]=p[i]+q[i];
    }
    bool flag=true;
    rep(i,1,n){
    if(mp[p[i]]) {flag=false;break;}
    if(p[i]<=0 || p[i]>n) {flag=false;break;}
    mp[p[i]]++;
    }
    if(!flag) cout<<-1<<'\n';
    else{
    rep(i,1,n) cout<<p[i]<<' ';
    } return 0;
    }

Codeforces Round #547 (Div. 3) C. Polycarp Restores Permutation (数学)的更多相关文章

  1. Codeforces Round #547 (Div. 3) 题解

    Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...

  2. Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)

    Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...

  3. Codeforces Round #622 (Div. 2) B. Different Rules(数学)

    Codeforces Round #622 (Div. 2) B. Different Rules 题意: 你在参加一个比赛,最终按两场分赛的排名之和排名,每场分赛中不存在名次并列,给出参赛人数 n ...

  4. Codeforces Round #284 (Div. 2)A B C 模拟 数学

    A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs

    F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...

  6. Codeforces Round #375 (Div. 2) C. Polycarp at the Radio 贪心

    C. Polycarp at the Radio time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  7. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集

    题目链接: 题目 F. Polycarp and Hay time limit per test: 4 seconds memory limit per test: 512 megabytes inp ...

  8. Codeforces Round #436 (Div. 2) B. Polycarp and Letters

    http://codeforces.com/contest/864/problem/B 题意: 给出一个字符串,要求找到一个集合S,使得从S中选出的所有数,在这些数的位置上的字母全部为小写且是不同的字 ...

  9. E. Superhero Battle Codeforces Round #547 (Div. 3) 思维题

    E. Superhero Battle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. iostat的输出

    第一行显示的时子系统启动以来的平均值,接下来的报告显示了增量的平均值,每个设备一行 Device:         rrqm/s   wrqm/s     r/s     w/s   rsec/s   ...

  2. 【Oracle】用sqlplus登录的各种方式

    1.本地登录 sqlplus / as sysdba 2.账号密码登录 sqlplus user/passwd 3.选择实例登录 sqlplus user/passwd@实例名   例如 sqlplu ...

  3. LeetCode108.有序数组转二叉搜索树

    题目 1 class Solution { 2 public: 3 TreeNode* sortedArrayToBST(vector<int>& nums) { 4 if(num ...

  4. 初次使用Open Live Writer

    关于下载和配置 建议大家不要在官网下载,会出不来.华军软件园(或其他下载站)也提供Open Live Writer最新版的下载. 创建账户时千万不要写错地址,错一个就失败. 体验 体验还是很好的,美中 ...

  5. Java并发包源码学习系列:阻塞队列BlockingQueue及实现原理分析

    目录 本篇要点 什么是阻塞队列 阻塞队列提供的方法 阻塞队列的七种实现 TransferQueue和BlockingQueue的区别 1.ArrayBlockingQueue 2.LinkedBloc ...

  6. 对于Spring MVC 拦截器的一些了解

    Spring MVC 拦截器的执行顺序 应用场景 假设请求 localhost:8080/ 则要求直接重定向到 localhost:8080/login ; 定义拦截器顺序 permission lo ...

  7. MapReduce过程源码分析

    MapReduce过程源码分析 Mapper   首先mapper完成映射,将word映射成(word,1)的形式.   MapReduce进程,Map阶段也叫MapTask,在MapTask中会通过 ...

  8. UT /SIT/ UAT

    UT /SIT/ UAT - 云+社区 - 腾讯云 https://cloud.tencent.com/developer/article/1541268 我们公司只有测试环境--准生产环境--生产环 ...

  9. Connection Pool

    MySQL :: MySQL Connector/NET Developer Guide :: 4.3 Managing a Connection Pool in Connector/NET http ...

  10. trust an HTTPS connection 安全协议 随机数 运输层安全协议 应用层安全协议 安全证书

    小结: 1.HTTPS存在不同于HTTP的默认端口及一个加密/身份验证层(在HTTP与TCP之间) HTTPS(全称:Hyper Text Transfer Protocol over Secure ...