链接:https://ac.nowcoder.com/acm/contest/5891/D
来源:牛客网
题目描述
One day, GK was getting very bored with palying stones. So he made a rule for himself:
There are n piles of stones in total, at least 0 stones in each pile. You can select a pile of stones each time you move, and then take out one stone from each other (n-1 in total) and put them into the selected pile. The number of stones in any pile at any time cannot be less than 0.That is to say, before you select a pile of stones to add to it, if there have a pile of 0 stones in other piles, you will not be able to select this pile of stones.(For example,there are 3 piles of stones,1 4 1,If you choose the second pile, it will become 0 6 0,and you won’t be able to do anything next )
GK wants to know if he can make every pile of stones equal by lots of operation as many times as he want. GK never does anything uncertain, so after a long meditation, he decides to ask for your help.
输入描述:
The first line is an integer T (1 ≤ T ≤ 1000), indicates that there are T-group data.
Each group of test data has two lines, the first line has an integer n (1≤n≤100), which means there are n piles of stones, the second line has n integers a1, a2…an (0 ≤ai ≤1000), which means the number of stones in each pile.
输出描述:
Each group of test data corresponds to an output. If GK can meet the requirements after any number of operations,print"Yes". Otherwise, print “No”.
There is no extra space at the beginning and end of the line, and each group of output takes up one line.
示例1
输入
复制
2
3
1 2 3
3
2 2 2
输出
复制
No
Yes
一开始这个题目没有任何的思路。不知道从哪一方面下手。。
对于这个序列中最小的那个数字,我们肯定是要匀给它一些石子的。经过这样若干次操作之后,在符合的条件下,就会达到平均。那么我们就模拟这个过程。这个题目数据量不是很大,就能过了。更好的方法暂时没有想到,希望路过的大佬可以指正。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=1e3+100;
int a[maxx];
vector<int> v;
int n;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
sort(a+1,a+1+n);
int sum=0;
for(int i=1;i<=n;i++) sum+=a[i];
if(sum%n) cout<<"No"<<endl;
else
{
v.clear();
for(int i=1;i<=n;i++) v.push_back(a[i]);
int flag=0;
int cnt=0;
while(cnt<=1500)
{
sort(v.begin(),v.end());
if(v[0]==v.back())
{
flag=1;
break;
}
if(v[1]==0) break;//出现0了,说明就要变成负数了,这样肯定不行。
v[0]+=(n-1);
for(int i=1;i<n;i++) v[i]--;
cnt++;
}
if(flag==0) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
}
return 0;
}
努力加油a啊,(o)/~