AtCoder Grand Contest 010 B - Boxes 验证解的存在性 2017/2/6
来源:互联网 发布:python黑客教程 编辑:程序博客网 时间:2024/05/29 09:57
题意:给定N个围成一圈的数,重复进行下述操作: 挑选第i个数,从i+1转一圈回到i,依次减去1到N。 问是否存在一种操作使得最后整圈的数全为0。
解题方法: 逆向思维好题! 将该过程倒着来看,就是每轮选择第i个数,从i+1转一圈回到i,依次加上1到N,将该操作设为操作1。问最后能否加到给定数列。令d[i]=a[i]-a[i-1],即考虑前数与后数之间的差,会发现,每次进行操作1时,差分的改变要么+1,要么-N+1,且两种情况里都有+1。故将差分减去s(操作的轮数),即处理掉+1的影响后。-N出现的次数恰为操作的轮数。
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int maxn = 100010;LL a[maxn];LL sum;int main(){ LL n; scanf("%lld", &n); for(int i = 1; i <= n; i++){ scanf("%lld", &a[i]); sum += a[i]; a[i-1] = a[i] - a[i-1]; } a[0] = a[0] - a[n]; if(n == 1){ printf("YES\n"); return 0; } if((sum*2)%(n*(n+1))){ printf("NO\n"); return 0; } LL ans = 0; LL cnt = sum / (n*(n+1)/2); for(int i = 0; i < n; i++){ a[i] -= cnt; if(a[i] % n || a[i] > 0){ printf("NO\n"); return 0; } ans += a[i] / (-n); } if(cnt == ans){ printf("YES\n"); } else{ printf("NO\n"); } return 0;}
0 0
- AtCoder Grand Contest 010 B - Boxes 验证解的存在性 2017/2/6
- AtCoder Grand Contest 010 B - Boxes 验证解的存在性
- AtCoder Grand Contest 010 B(差分)
- AtCoder Grand Contest 010
- AtCoder Grand Contest 010
- AtCoder Grand Contest 012 B
- AtCoder Grand Contest 017-B
- Atcoder Grand Contest 012 B
- AtCoder Grand Contest 019 B
- 构造——AtCoder Grand Contest 010 B
- AtCoder Grand Contest 010D
- AtCoder Grand Contest 010 F
- AtCoder Grand Contest 011
- AtCoder Grand Contest 018
- AtCoder Grand Contest 018
- Atcoder Grand Contest 019
- AtCoder Grand Contest 008
- AtCoder Grand Contest 005【A栈模拟,B单调栈】
- 说说程序员不解风情的瞬间
- java编程思想2
- IO流_字符流复制文本文件案例3
- 2017年规划
- 【bzoj2242】【SDOI2011】计算器
- AtCoder Grand Contest 010 B - Boxes 验证解的存在性 2017/2/6
- 什么是双绞线
- 51NOD 1682 中位数计数
- 理解 Java 的 GC 与 幽灵引用
- HTML5 & CSS3初学者指南(4) – Canvas使用
- framebuffer之s3cfb_probe分析
- Total Order Partitioner
- rsync 命令详解
- 往返狗遇人次数问题