枚举第一个人对第二个人的三种操作,
然后这样第i个人必然对第i+1个人按照1.少一个糖果就从下一个人手中拿一个
2.多一个就给一个给下一个人
3.刚好就是平均数跳过
4.无解
这四种操作贪心即可。
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
int a[100010],b[100010];
vector<int>ans;
bool isok(int n,int av)
{
for(int i=0;i<n;i++)
b[i]=a[i];
for(int i=1;i<n;i++)
{
int t=b[i]-av,j=(i+1)%n;
if(t==-1)
{
b[i]++;
b[j]--;
ans.push_back(j);
ans.push_back(i);
}
else if(t==0)
continue;
else if(t==1)
{
b[i]--;
b[j]++;
ans.push_back(i);
ans.push_back(j);
}
else
return 0;
}
for(int i=0;i<n;i++)
if(b[i]!=av)
return 0;
return 1;
}
void print()
{
puts("YES");
cout<<ans.size()/2<<endl;
for(int i=0;i<ans.size();i+=2)
cout<<ans[i]+1<<" "<<ans[i+1]+1<<endl;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
ll sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",a+i);
sum+=a[i];
}
if(sum%n!=0)
{
puts("NO");
continue;
}
sum/=n;
int av=int(sum);
ans.clear();
if(isok(n,av))
print();
else
{
a[0]--;a[1]++;
ans.clear();
ans.push_back(0);
ans.push_back(1);
if(isok(n,av))
print();
else
{
a[0]+=2;a[1]-=2;
ans.clear();
ans.push_back(1);
ans.push_back(0);
if(isok(n,av))
print();
else
puts("NO");;
}
}
}
}
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 127 Accepted Submission(s): 16
Special Judge
Problem Description
There are
n
soda sitting around a round table. soda are numbered from
1
to
n
and
i
-th soda is adjacent to
(i+1)
-th soda,
1
-st soda is adjacent to
n
-th soda.
Each soda has some candies in their hand. And they want to make the number of candies the same by doing some taking and giving operations. More specifically, every two adjacent soda x and y can do one of the following operations only once:
1. x -th soda gives y -th soda a candy if he has one;
2. y -th soda gives x -th soda a candy if he has one;
3. they just do nothing.
Now you are to determine whether it is possible and give a sequence of operations.
Each soda has some candies in their hand. And they want to make the number of candies the same by doing some taking and giving operations. More specifically, every two adjacent soda x and y can do one of the following operations only once:
1. x -th soda gives y -th soda a candy if he has one;
2. y -th soda gives x -th soda a candy if he has one;
3. they just do nothing.
Now you are to determine whether it is possible and give a sequence of operations.
Input
There are multiple test cases. The first line of input contains an integer
T
, indicating the number of test cases. For each test case:
The first contains an integer n (1≤n≤105) , the number of soda.
The next line contains n integers a1,a2,…,an (0≤ai≤109) , where ai denotes the candy i -th soda has.
The first contains an integer n (1≤n≤105) , the number of soda.
The next line contains n integers a1,a2,…,an (0≤ai≤109) , where ai denotes the candy i -th soda has.
Output
For each test case, output "YES" (without the quotes) if possible, otherwise output "NO" (without the quotes) in the first line. If possible, then the output an integer
m
(0≤m≤n)
in the second line denoting the number of operations needed. Then each of the following
m
lines contain two integers
x
and
y
(1≤x,y≤n)
, which means that
x
-th soda gives
y
-th soda a candy.
Sample Input
3 6 1 0 1 0 0 0 5 1 1 1 1 1 3 1 2 3
Sample Output
NO YES 0 YES 2 2 1 3 2
Source