AtCoder Grand Contest 010—Boxes

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Seeyouer/article/details/80052302

题目描述

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==5
肯定没一次都加 1,2,3,4,5
只不过顺序不同而已
假设有m次,m==3
举一个例子
1 2 3 4 5
5 1 2 3 4
3 4 5 1 2


9 7 10 8 11
如果最后他们的和sum/m==(n*(n+1)/2) ,则有可能正确,否则一定不可能!
因为每一次后面-前面的值都是1或1-n,且每一次有(n-1)个1,1个(1-n)
只要根据最后和sum【】的前后差找出1的个数和(1-n)的个数对应就可以

注意:差不会超过m!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[111111],d[111111];
int main()
{
    ll n;cin>>n;
    ll sum=0;
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        sum+=a[i];
        d[i]=a[i]-a[i-1];
    }
    d[1]=a[1]-a[n];
    if(n==1){
        cout<<"YES\n";return 0;
    }
    if(sum%((n+1)*n/2)!=0){
        printf("NO\n");return 0;
    }

    ll m=sum/((n+1)*n/2),flag=1,t=0,s=0;
    for(int i=1;i<=n;i++){
        if(d[i]>m){
            flag=0;break;
        }
        if(d[i]==m){
            t+=m;
        }
        else{
            s+=(m-d[i])/n;//假设这一列有x个(1-n),则有(m-x)个1,和为d[i]
            t+=(m-((m-d[i])/n));
        }
        //cout<<s<<" "<<t<<endl;
    }

    if(t/(n-1)==s&&flag){
        printf("YES\n");
    }
    else printf("NO\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Seeyouer/article/details/80052302
今日推荐