问题 K: Boxes
时间限制: 2 Sec 内存限制: 256 MB提交: 345 解决: 82
[ 提交][ 状态][ 讨论版][命题人: admin]
题目描述
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
输入
N
A1 A2 … AN
输出
样例输入
5
4 5 1 2 3
样例输出
YES
提示
All the stones can be removed in one operation by selecting the second box.
这题的确很有趣:
问题很简单就是有 1~n的位置 围着一个圈顺时针 就把它当作盒子吧
(英语不好)。。。盒子里装着a[1~n]个石头。
(以某一处为起点从盒子里拿东西以起点距离)那拿 (距离+1)个石头。
就是 6 3 5 7 9
1、- 1 2 3 4 5
2、- 5 1 2 3 4
结果 0 0 0 0 0
所以是YES
这题我当时也没做出来,然后回来时队友就做出来了。
后来问了扬哥哥才知道怎么一回事。
其实石子里的大小是不确定的,因为每一个位置放的石头个数不同。
这道题首先是逆向来想,题目要你一圈一圈地解开,你可以变成一圈一圈加上去。
后来你就会发现石头个数变化你是无法捉摸,而石头前后的差值你却可以确定。
因为题目固定了方向,石头的差值:D[ i ]=a[ i ]-a[ i-1 ] ,(i!=1);
D[1]=a[ 1 ]-a[ n ];
加一圈其实差值只有两个可能: A、1 B、1-n
其实你可以通过 sum= { [ (1+n)×n ] / 2 } × T;
算出来 次数T 对于这些差值减去T。
例如 1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
得到 6 9 12 10 8
D[1~5]: -2 3 3 -2 -2
减去T: -5 0 0 -5 -5
得到的只可能是非正数且能%n==0.
具体可以看代码:
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
ll n,a[100050],d[100050],sum=0;
scanf("%lld",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
if(i!=1){
d[i]=a[i]-a[i-1];
}
sum+=a[i];
}
d[1]=a[1]-a[n];
if(sum%(n*(n+1)/2)==0){
ll t=sum/(n*(n+1)/2);
for(int i=1;i<=n;i++){
d[i]-=t;
}
int flag=1;
for(int i=1;i<=n;i++){
if( d[i]<=0 && (d[i])%n==0){
continue;
}else{
flag=0;break;
}
}
if(flag)
printf("YES\n");
else{
printf("NO\n");
}
}else{
printf("NO\n");
}
return 0;
}