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:

N
A1 A2AN

Output

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


Sample Input 1

5
4 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

5
6 9 12 10 8

Sample Output 2

YES

Sample Input 3

4
1 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;
}
时间: 2025-01-20 01:13:35

Boxes的相关文章

uva 12657 - Boxes in a Line(AC和TLE的区别,为什么说STL慢..)

用STL中的list写的,TLE #include<cstdio> #include<iostream> #include<cstring> #include<list> #include<algorithm> using namespace std; list<int> l; list<int>::iterator it1,it2,it3,it4,it5,it; void work(int a,int a1=1,int

[LeetCode] Remove Boxes 移除盒子

Given several boxes with different colors represented by different positive numbers. You may experience several rounds to remove boxes until there is no box left. Each time you can choose some continuous boxes with the same color (composed of k boxes

UVa 12657 Boxes in a Line(应用双链表)

Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4 kinds of commands: ? 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y ) ? 2 X Y : move box X to th

UVa - 103 - Stacking Boxes

Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and analyzing the topology o

poj 1475 Pushing Boxes

http://poj.org/problem?id=1475 Pushing Boxes Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 4662   Accepted: 1608   Special Judge Description Imagine you are standing inside a two-dimensional maze composed of square cells which may or

HDU 1475 Pushing Boxes

Pushing Boxes Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on PKU. Original ID: 147564-bit integer IO format: %lld      Java class name: Main Special Judge Imagine you are standing inside a two-dimensional maze composed of sq

uva103 - Stacking Boxes(DAG)

题目:uva103 - Stacking Boxes(DAG) 题目大意:给出N个boxes, 并且给出这些箱子的维度,要求找一个最长的序列,能够使得下面的箱子一定能够有个维度序列大于上面的那个箱子的维度序列.例如:A箱子(2 3 4),B箱子(3 4 5),因为有个序列2 3 4 , 3 4 5使得B每个维度的值都大于A,所以A可以在B上面 . 解题思路:DAG.将这些箱子哪个能在哪个上面处理出有向图出来,这里判断是否可以在上面的情况,只要将这两个箱子的维度都从小到大排下序,然后比较一下是否对

UVa 12657 Boxes in a Line(双向链表的应用)

Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4 kinds of commands: ? 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y ) ? 2 X Y : move box X to th

hdu-5810 Balls and Boxes(概率期望)

题目链接: Balls and Boxes Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description Mr. Chopsticks is interested in random phenomena, and he conducts an experiment to study randomness. In the experiment, he

O - Boxes in a Line

O - Boxes in a Line Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4 kinds of commands: • 1 X Y : move box X t