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。

思路:如果可以将全部石头移走的话那么将是全部的石头加起来满足前n项和或者满足前n项和的倍数,  其次如果这些是头可以全部移走的两者之间的差值将会是1或者1-n根据这个用一个数组vis将两个之间的差值保存一下,之后再用for判断就可以了,这里需要注意的一点是我提交了好几代码都不对,后来发现是求前n项和的时候没有用longlong 。




#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<iostream>
#include<map>
#define mes(a,b) memset(a,b,sizeof(a))
#define rep(i,m,n) for(i=m;i<=n;i++)
typedef long long ll;
using namespace std;
int max3(int a,int b,int c){return max(max(a,b),c);}
ll min3(ll a,ll b,ll c){return min(min(a,b),c);}
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll Fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n;n=n*n;}return r;}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

int n;
int a[100005],vis[100005];


int main(){
    while(cin>>n)
    {
        ll sum=0,sum2,t;
        bool flag=1;
        memset(vis,0,sizeof(vis));
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];

        }
         sum2=(ll)((1+n))*n/2;
        if(sum%sum2!=0)
        {
            printf("NO\n");
            continue;
        }


        t=sum/sum2;//printf("%d\n",t);
        a[n]=a[0];
        for(int i=0; i<n; i++)
            vis[i]=a[i+1]-a[i];
        for(int i=0; i<n; i++)
        {
            if((vis[i]-t)%n!=0 || vis[i]-t>0)
            {
                flag=0;
                break;
            }


        }
        if(flag==0)
            printf("NO\n");
        else
            printf("YES\n");
    }

    return 0;
}

Pushing Boxes

03-03

Scrap cars in a junk yard are crushed in a device that pushes the car in from the sides, from the front and back, and from the top and bottom. The result is a compact little chunk of metal. In this problem you're going to model a device that works in a similar manner, but doesn't crush anything, only pushes boxes around in two dimensions. The boxes are all square with unit length on a side and are situated on the floor of a room. Each wall of the room can be programmed to move inward a certain amount, pushing any boxes it may bump into. Unlike the car-crusher, this device is sensitive and if it senses that boxes are stacked up against a wall and that it might crush them if pressed any farther, it will stop.nnnnFor example, suppose we have boxes arranged in a 12-by-16 room as shown below. The upper left-hand corners of the boxes (which is how we will locate them in this problem) are at coordinates (1,13) (box A below), (3,2), (6,2), (6,4), (6,6), (7,6) and (8,9) (box G), where the first coordinate indicates distance from the top wall and the second coordinate indicates distance from the left wall.nnSuppose the top wall is programmed to move down 3 units (then retreats, as the walls always will) and then the right wall is programmed to move left 14 units. The first operation can be performed with no problem, but the second one can not be carried out without crushing some boxes. Therefore, the right wall will move only 13 units, the maximum distance it can move until boxes are packed tightly between it and the left wall. The boxes will then be in the configuration shown in the following figure. The locations of the boxes are (3,1), (3,2), (6,0), (6,1), (6,2), (7,2), (8,2).nnnnInputnnThere will be multiple data sets for this problem. The first line of each data set will be two integers giving the height and width of the room. (We'll visualize the room as if on a piece of paper, as drawn above.) Each dimension will be no more than 20. The next line will contain an integer n (0 < n <= 10) followed by n pairs of integers, each pair giving the location of a box as the distances from the top and the left walls of the room. The following lines will be of the form direction m, where direction is either down, left, up, right, or done and m is a positive integer. For example, left 2 would mean to try to move the right wall 2 spaces to the left. The "direction" done indicates that you are finished pushing this set of boxes around. There will be no integer m following the direction done, of course. The data sets are followed by a line containing 0 0.nnnOutputnnFor each data set you are to produce one line of output of the form:nnData set d ends with boxes at locations (r1, c1) (r2, c2) . . . (rn, cn).nnwhere the (ri, ci) are the locations of the boxes given from top-to-bottom, left-to-right, (separated by one space) and d is the data set number (starting at 1).nnnSample Inputnn12 16n7 1 13 3 2 6 2 6 4 6 6 7 6 8 9ndown 3nleft 14ndonen4 4n3 1 0 2 1 2 3nright 3nup 2nleft 1ndonen0 0nnnSample OutputnnData set 1 ends with boxes at locations (3,1) (3,2) (6,0) (6,1) (6,2) (7,2) (8,2).nData set 2 ends with boxes at locations (0,2) (1,1) (1,2).n 问答

没有更多推荐了,返回首页