Atcoder Grand Contest 010 B - Boxes 差分
B - Boxes
题目连接:
http://agc010.contest.atcoder.jp/tasks/agc010_b
Description
There are N boxes arranged in a circle. The i-th box contains Ai stones.
Determine whether it is possible to remove all the stones from the boxes by repeatedly performing the following operation:
Select one box. Let the box be the i-th box. Then, for each j from 1 through N, remove exactly j stones from the (i+j)-th box. Here, the (N+k)-th box is identified with the k-th box.
Note that the operation cannot be performed if there is a box that does not contain enough number of stones to be removed.
Input
1≦N≦105
1≦Ai≦109
The input is given from Standard Input in the following format:
N
A1 A2 … AN
Output
If it is possible to remove all the stones from the boxes, print YES. Otherwise, print NO.
Sample Input
5
4 5 1 2 3
Sample Output
YES
Hint
题意
给你n个数,你可以操作任意次,每次操作你可以选择一个数,使得离这个数距离为i的数-i。
问你最后是否能够使得所有数都变成0.
题解:
每次操作会减去1+2+....+n=n(n+1)/2,所以至少sum%(n(n+1)/2)==0
满足这个条件之后,我们发现是一个等差数列递减的,那么我们差分一下,就变成全部减一了。但是!最后一个数和第一个数之间的差值,却增加了(-(n-1)),这个推一下就知道了。
那么差分后的d[i]=a[i+1]-a[i],必须满足d[i] − (k − x) + (n − 1)x = 0,其中k为总共操作次数,x为在这个位置的操作次数,化简后得到k-d[i]=nx,那么我们只需要check(k-d[i]%n==0)就好了。
智商题……
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n;
long long a[maxn],b[maxn],sum;
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lld",&a[i]),sum+=a[i];
long long t=1ll*n*(n+1)/2;
if(sum%t){
cout<<"NO"<<endl;
return 0;
}
long long cnt = sum/t;
for(int i=0;i<n;i++){
b[i]=(a[(i+1)%n]-a[i]-cnt);
}
bool flag = true;
for(int i=0;i<n;i++){
if(b[i]>0||(-b[i])%n)
flag = false;
}
puts(flag?"YES":"NO");
}
Atcoder Grand Contest 010 B - Boxes 差分的更多相关文章
- AtCoder Grand Contest 010
AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...
- Atcoder Grand Contest 010 C - Cleaning 树贪心(伪)
C - Cleaning 题目连接: http://agc010.contest.atcoder.jp/tasks/agc010_c Description There is a tree with ...
- AtCoder Grand Contest 010 C:Cleaning
题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_c 题目翻译 给你一棵树,每个点有个权值,每次操作可以选择两个度数为\(1\)的结点,然后让这 ...
- AtCoder Grand Contest 010 F - Tree Game
题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_f 题目大意: 给定一棵树,每个节点上有\(a_i\)个石子,某个节点上有一个棋子,两人轮流操 ...
- AtCoder Grand Contest 010题解
传送门 \(A\) 判一下奇数的个数就行了 const int N=1e5+5; int a[N],n,res; int main(){ scanf("%d",&n); f ...
- AtCoder Grand Contest 010 D - Decrementing
题目描述 有n个整数,其中第i个数为Ai.这些数字的gcd为1.两人轮流操作,每次操作把一个大于1的数减1,并把所有数除以所有数的最大公约数,最后无法操作者输,求是否先手必胜. 如果当前的sum为偶数 ...
- AtCoder Grand Contest 006
AtCoder Grand Contest 006 吐槽 这套题要改个名字,叫神仙结论题大赛 A - Prefix and Suffix 翻译 给定两个串,求满足前缀是\(S\),后缀是\(T\),并 ...
- Atcoder Grand Contest 036 D - Negative Cycle
Atcoder Grand Contest 036 D - Negative Cycle 解题思路 在某些情况下,给一张图加或删一些边要使图合法的题目要考虑到最短路的差分约束系统.这一题看似和最短路没 ...
- AtCoder Grand Contest 012
AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...
随机推荐
- bzoj千题计划295:bzoj3140: [Hnoi2013]消毒
http://www.lydsy.com/JudgeOnline/problem.php?id=3140 如果只有两维,那就是二分图最小点覆盖 现在是三维,但是a*b*c<=5000,说明最小的 ...
- mysql出现ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' 错误
init 神奇..其他的都没有成功,这个居然成功了!! 还试验过:sudo mysqld restart啥的,都没有用......
- c#的事件用法——实现下载时发生的事件
//下载时发出的事件 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...
- 从ACM会议分析我国计算机科学近十年发展情况
从ACM会议分析我国计算机科学近十年发展情况 来源:<中国计算机学会通讯>2015年第10期<专栏> 作者:陈 钢 2006年,承蒙李国杰院士推荐,<中国计算机学会通讯& ...
- caoha
- 【Pyhon】获取文件MIME类型,根据文件类型自定义文件后缀
场景 下载样本,都是MD5命名的无后缀文件,需要自己手动查询然后修改文件后缀. 根据文件类型自定义后缀可以很方便地根据后缀判断用什么工具分析. 使用说明 libmagic 地址:https://pyp ...
- PHP中VC6、VC9、TS、NTS版本区别与用法
1. VC6与VC9的区别: VC6 版本是使用 Visual Studio 6 编译器编译的,如果你的 PHP 是用 Apache 来架设的,那你就选择 VC6 版本. VC9 版本是使用 Vis ...
- MAVEN:不能互相引用
工程A,工程B,工程C,这三个工程:C依赖B,B依赖A,这是没有问题的. 但是不能A依赖B,B又依赖A,这是不允许的.
- 四、vue语法补充
1.自定义过滤器 格式: {{ msg | filters}} 2.computed 属性默认只有 getter ,不过在需要时你也可以提供一个 setter <!DOCTYPE html> ...
- liunx 安装和解压命令
tar.gz格式 sudo tar -xvf file.tar.gz -C /目录/ //注意C为大写,-C /目录是可选的 zip sudo jar xvf file.zip //jar ...