Atcoder Grand Contest 010 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 D - Decrementing
题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_d 题目大意: 有\(n\)个数\(A_i\),它们的\(gcd\)是1,A.B两人轮流操作, ...
- 【每日亿题#12】AtCoder Grand Contest 021 (A ~ F)全部题解
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 文章目录 AtCoder Grand Contest 021 题解 A. Digit Sum 2 B. ...
- AtCoder Grand Contest 008: Contiguous Repainting(思维)
Contiguous Repainting 时间限制: 2 Sec 内存限制: 256 MB 提交: 69 解决: 22 [提交][状态][讨论版][命题人:admin] 题目描述 There a ...
- AtCoder Grand Contest 017
AtCoder Grand Contest 017 A - Biscuits 有\(n\)个数,问有多少个集合的数的和模\(2\)余\(P\). 随便\(dp\)一下就好了. #include< ...
- AtCoder题解——AtCoder Grand Contest 048——A - atcoder < S
题目相关 题目链接 AtCoder Grand Contest 048 A 题,https://atcoder.jp/contests/agc048/tasks/agc048_a. Problem S ...
- AtCoder题解 —— AtCoder Grand Contest 050 —— B - Three Coins —— 动态规划
题目相关 题目链接 AtCoder Grand Contest 050 B 题,https://atcoder.jp/contests/agc050/tasks/agc050_b. Problem S ...
- Atcoder Grand Contest 012 B - Splatter Painting解题报告
题目:http://agc012.contest.atcoder.jp/tasks/agc012_b 有一个n点m边的图,(不一定联通) 还有q个操作:每次将一个点v及其周围距离<=d的点涂成颜 ...
- [atcoder]AtCoder Grand Contest 027题解
[题目链接] https://agc027.contest.atcoder.jp/ A [题解] 题意: 是把xxx个糖果分给nnn个人,一个人如果恰好分到aia_{i}ai个糖果就会高兴.求最多使 ...
- UPC个人训练赛第十五场(AtCoder Grand Contest 031)
传送门: [1]:AtCoder [2]:UPC比赛场 [3]:UPC补题场 参考资料 [1]:https://www.cnblogs.com/QLU-ACM/p/11191644.html B.Re ...
最新文章
- java判断键盘按键按下_JAVA 从键盘输入一个字符串 判断其是否能被转换为一个有效的数字,字符串可以按位取出...
- 广州自由自在进口食品休闲消费的趋势
- POJ 2195 Going Home / HDU 1533(最小费用最大流模板)
- 如何用postman发送新浪微博
- 北邮-上机-提交错误解决及一些经验
- go 判断切片是否存在某元素_Golang基础之切片
- python学习(1)启程
- Python获取sklearn库中iris数据写入本地csv文件,可视化展示数据并进行分类、聚类实验以及结果可视化
- Django Ajax文件下载
- Cypress 简介
- 【BUG】Python3|爬虫请求得到的json中的值全是问号
- 乐山计算机学校有几个,乐山计算机学校全国排名是多少,好不好?
- 《牧羊少年奇幻之旅》读后感
- ZT210打印标签方法及常见问题20230110
- 朝鲜APT组织使用带后门IDA软件攻击安全研究人员
- Linux系统中的磁盘管理
- VTM5中的Implicit MTS和Explicit MTS
- SaaS将导航中小企业
- 可以免费申请https证书的网址
- 第一章 1.11 高阶函数
热门文章
- 设计模式--观察者模式--Java实现
- Android中Adapter的notifyDataSetInvalidated()和notifyDataSetChanged()的区别
- 联想服务器改win7系统教程,联想IdeaCentre720改win7系统教程及BIOS设置方法
- Go 如何利用 Linux 内核的负载均衡能力
- asp.net mvc5 oracle,ASP.NET MVC5网站开发用户注册(四)
- Java毕设项目:垃圾分类(带源码和搭建过程)
- springsecurity 认证之授权码模式
- activity任意节点跳转
- 【转】C/C++的64位整型 不同编译器间的比较
- (新聞) 友達光電 A+種子暑期實習計畫 埋下希望的光電種子 (日記)