本文主要是介绍Boxes(思维+差分数组),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
问题 K: Boxes
时间限制: 2 Sec 内存限制: 256 MB提交: 254 解决: 52
[ 提交][ 状态][ 讨论版][命题人: 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.
题意:有n个盒子,每个盒子里面有ai个石头,每次操作可选定一个i,然后j从1-n循环,从编号为i+j的盒子里面拿j个石头,问是否有一个操作顺序可以正好拿完所有盒子中的石头。
题解:每次操作,拿走的石头数量是固定的,一定是(1+2+3+...+n)个石头,所以对于盒子中石头的总数一定要是(1+2+3+....+n)的倍数,是几倍就是需要操作几次,每一次操作除最后一个和第一个外,其余的后一个拿的石头都比前一个多1,拿t次则多t,再特判最后一个即可。
#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}int a[100005],b[100005];
int main()
{int n;scanf("%d",&n);ll temp=1LL*(1+n)*n/2;ll sum=0;fo(i,1,n) scanf("%d",&a[i]),sum+=a[i];if(sum%temp!=0){printf("NO\n");return 0;}ll cot=sum/temp;b[1]=a[1]-a[n]-cot;fo(i,2,n) b[i]=(a[i]-a[i-1]-cot);for(int i=1;i<=n;i++)if(b[i]>0 || (-b[i])%n>0){printf("NO\n");return 0;}printf("YES\n");return 0;
}
这篇关于Boxes(思维+差分数组)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!