首页 > 代码库 > Boxes
Boxes
Boxes
Time limit : 2sec / Memory limit : 256MB
Score : 500 points
Problem Statement
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.
Constraints
- 1≦N≦105
- 1≦Ai≦109
Input
The input is given from Standard Input in the following format:
NA1 A2 … AN
Output
If it is possible to remove all the stones from the boxes, print YES
. Otherwise, print NO
.
Sample Input 1
54 5 1 2 3
Sample Output 1
YES
All the stones can be removed in one operation by selecting the second box.
Sample Input 2
56 9 12 10 8
Sample Output 2
YES
Sample Input 3
41 2 3 1
Sample Output 3
NO
分析:首先可以根据总和得出操作次数p,然后考虑相邻的两个数;
a[i]-a[i+1]=(n-1)x-(p-x);(a[i]=n,a[i+1]=1贡献1-n,a[i]=y,a[i+1]=y+1贡献-1);
化简即a[i]-a[i+1]=nx-p;
检验(a[i]-a[i+1]+p)%n==0即可;
代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <climits>#include <cstring>#include <string>#include <set>#include <bitset>#include <map>#include <queue>#include <stack>#include <vector>#define rep(i,m,n) for(i=m;i<=n;i++)#define mod 1000000007#define inf 0x3f3f3f3f#define vi vector<int>#define pb push_back#define mp make_pair#define fi first#define se second#define ll long long#define pi acos(-1.0)#define pii pair<int,int>#define sys system("pause")const int maxn=1e5+10;using namespace std;inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}inline void umax(ll &p,ll q){if(p<q)p=q;}inline void umin(ll &p,ll q){if(p>q)p=q;}inline ll read(){ ll x=0;int f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f;}int n,m,k,t,a[maxn];int main(){ int i,j; scanf("%d",&n); ll ret=0,ok=0; rep(i,1,n)scanf("%d",&a[i]),ret+=a[i],ok+=i; if(ret%ok!=0)return 0*puts("NO"); ll x=ret/ok,p=0; a[n+1]=a[1]; rep(i,1,n) { ll q=a[i]-a[i+1],qq=(q+x)/n;; if(qq*n-x!=q)return 0*puts("NO"); if(qq<0||qq>x)return 0*puts("NO"); p+=qq; } if(p!=x)return 0*puts("NO"); puts("YES"); return 0;}
Boxes
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。