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\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...
随机推荐
- Java入门系列(九)Java API
String,StringBuilder,StringBuffer三者的区别 1.首先说运行速度,或者说是执行速度 在这方面运行速度快慢为:StringBuilder > StringBuffe ...
- [整理]javascript压缩、格式化
1.使用packer来压缩JS文件 packer工具在线版:http://dean.edwards.name/packer/ 通过packer对js打包压缩的同时,执行Base62 encode编码后 ...
- JavaScript 删除 ASP.NET 设置的多值 Cookie 的方法
需要注意HttpOnly,Path等属性.完整的测试代码: ASPX 代码<%@ Page Language="C#" %> <!DOCTYPE html PUB ...
- ASP.NET乱码深度剖析
写在前面 在Web开发中,乱码应该算一个常客了.今天还好好的一个页面,第二天过来打开一看,中文字符全变“外星文”了.有时为了解决这样的问题,需要花上很长的时间去调试,直至抓狂,笔者也曾经历过这样的时期 ...
- JavaScript编写风格指南 (二)
七:注释 // 频繁的使用注释有助于他人理解你的代码// 1.代码晦涩难懂// 2.可能被误认为是错误的代码// 3.必要但不明显的针对特定浏览器的代码// 4.对于对象,方法或者属性,生成文档是有必 ...
- CS229 笔记06
CS229 笔记06 朴素贝叶斯 事件模型 事件模型与普通的朴素贝叶斯算法不同的是,在事件模型中,假设文本词典一共有 \(k\) 个词,训练集一共有 \(m\) 封邮件,第 \(i\) 封邮件的词的个 ...
- Passbook
CHENYILONG Blog Passbook 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilon ...
- linux笔记_day12_shell编程
1.shell中如何进行算术运算 A=1 B=2 1)let 算术运算表达式 let C=$A+$B 2)$[算术运算表达式] C=$[$A+$B] 3)$(($A+$B)) 4) expr 算术表达 ...
- Android 4.4 API
Android 4.4 (KITKAT) 是新的 Android 平台版本,为用户和应用开发者提供了新功能.本文旨在介绍其中最值得关注的新 API. 作为应用开发者,您应尽快从 SDK 管理器下载 A ...
- mybatis generator 双击创建失败
https://coding.imooc.com/learn/questiondetail/20460.html 用的下面这个方法