Boxes

题目描述

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

输入

The input is given from Standard Input in the following format:

N
A1 A2 … AN

输出

If it is possible to remove all the stones from the boxes, print YES. Otherwise, print NO.

样例输入

5
4 5 1 2 3

样例输出

YES

提示

All the stones can be removed in one operation by selecting the second box.

题意:

有N个盒子,每个盒子有Ai块石头,每次操作可以选一个盒子i,然后对于每个j(1 <= j <= N),从第(i+j)个盒子移走j个石头(第N+k个盒子即为第k个盒子),成一个环状。如果存在一个盒子的石头数目不够操作将不能进行。问能否将所有的石头移走,能的话,输出YES,反之,输出NO.

思路:

成环状,sum=总石头数,首先不知道每次的起点,但每次循环,移走的石头数是从1到n,即ans+=i;可以用公式n*(n+1)/2;所以可以判断总的石头数sum%ans,若不为0,输出NO;轮数=sum/ans,看相邻两个石头差值,如果比轮数少或轮完后有剩余,输出NO.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100005],b[100005];
int main(){
    ll ans,sum;
    ans = 0; sum = 0;
    int n;
    scanf("%d",&n);
    int flag = 1;
    for(int i = 0 ; i < n ; i ++){
        scanf("%lld",&a[i]);
        sum += a[i];
        ans += i+1;
    }
    if(sum%ans)
        flag = 0;
    else{
        ll y = sum/ans;
        for(int i = 0; i < n; i ++)
            b[i] = a[(i+1)%n]-a[i];
        for(int i = 0; i < n; i ++){
            if(b[i]-y>0||(b[i]-y)%n<0){
                flag = 0;
                break;
            }
        }
    } 
    if(flag)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值