Average
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1948 Accepted Submission(s): 495
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
1. x
2. y
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≤10
5
)
, the number of soda.
The next line contains n
integers
a
1
,a
2
,…,a
n![]()
(0≤a
i
≤10
9
)
, where
a
i![]()
denotes the candy
i
-th soda has.
The first contains an integer n
The next line contains n
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
题意:有n个人围成一圈,每个人手上都有糖果数量,每个人可以给相邻的人一个糖果且只能给一次,问能否使得每个人手上的糖果一样多,如果可以输出YES 并输出操作步聚,如果不行输出NO。
解题:分情况:avg:平均值。a[ i ] :第 i 个人手上的糖果
1.a[1]-avg==2:向左右两给一个糖果,然后从1->n进行判断操作。
2.a[1]-avg==-2:从左右各得到一个糖果,然后............................
3.a[1]-avg==1:(1):向第2个人给一个糖果,然后............。(2)向第n个人给一个糖果,然后…………。
4.a[1]-avg==-1:(1):从第2个人得到一个糖果,然后……。(2)从第n个人得到一个糖果,然后……。
5.a[1]-avg==0:(1)第1个什么都不做,然后.......……。(2)从第2个人过寄一个糖果给第1个人,再第1个人又给了第n个人,然后…………。(3)从第n个人过寄一个糖果给第1个人,再第1个人又给了第2个人,然后…………。
当然,如果再某个情况找到了就可以直接输出。
#include<stdio.h>
#include<string.h>
#define ll __int64
const int N = 100005;
struct EDG{
int x,y;
};
int n,step;
ll a[N],b[N],avg;
EDG edg[N<<1];
void addEdg(int x,int y)
{
edg[step].x=x;
edg[step].y=y;
step++;
}
bool findout(int l)
{
while(l<=n){
if(a[l]==avg){l++;continue;}
if(l==n)return 0;
if(a[l]>avg){
if(a[l]-avg>1){
return 0;
}
else{
l++;
a[l]++;
a[l-1]--;
addEdg(l-1 , l); //从左向右过一个
}
}
else {
if(avg-a[l]>1){
return 0;
}
if(a[l+1]<avg){
return 0;
}
l++; a[l]--; a[l-1]++;
addEdg(l , l-1); //从右向左过一个
}
}
return 1;
}
void print()
{
printf("YES\n%d\n",step);
for(int i=0; i<step; i++)
printf("%d %d\n",edg[i].x,edg[i].y);
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
avg=0;
for(int i=1; i<=n; i++)
{
scanf("%I64d",&b[i]);
avg+=b[i] ;
}
if(avg%n!=0){
printf("NO\n");continue;
}
avg/=n;
step=0;
//因是一个圈,所以从第1 个分情况讨论,注意每次求完一轮把step 初始为0,我就是在这个WA了好多次
if(b[1]-avg==2){
for(int i=1; i<=n; i++)
a[i]=b[i];
a[1]--; a[2]++; addEdg(1 , 2);
if(n>2){
a[1]--;a[n]++; addEdg(1, n);
if( findout(1) ){
print(); continue;
}
}
}
else if(b[1]-avg==-2){
if(b[2]&&b[n]){
for(int i=1; i<=n; i++)
a[i]=b[i];
a[1]++; a[2]--; addEdg(2 , 1);
if(n>2){
a[1]++;a[n]--; addEdg(n, 1);
if( findout(1) ){
print(); continue;
}
}
}
}
else if(b[1]-avg==1){
for(int i=1; i<=n; i++)
a[i]=b[i];
a[1]--; a[2]++; addEdg(1 , 2);
if( findout(1) ){
print(); continue;
}
step=0;
for(int i=1; i<=n; i++)
a[i]=b[i];
a[1]--; a[n]++; addEdg(1 , n);
if( findout(1) ){
print(); continue;
}
}
else if(b[1]-avg==-1){
if(b[2]){
for(int i=1; i<=n; i++)
a[i]=b[i];
a[1]++; a[2]--; addEdg(2 , 1);
if( findout(1) ){
print(); continue;
}
}
step=0;
if(b[n]){
for(int i=1; i<=n; i++)
a[i]=b[i];
a[1]++; a[n]--; addEdg(n , 1);
if( findout(1) ){
print(); continue;
}
}
}
else if(avg==b[1]){
for(int i=1; i<=n; i++)
a[i]=b[i];
if( findout(1) ){
print(); continue;
}
step=0;
if(b[2]){
for(int i=1; i<=n; i++)
a[i]=b[i];
a[n]++; a[2]--; addEdg(1 , n); addEdg(2 , 1);
if( findout(1) ){
print(); continue;
}
}
step=0;
if(b[n]){
for(int i=1; i<=n; i++)
a[i]=b[i];
a[n]--; a[2]++;addEdg(1 , 2); addEdg(n , 1);
if( findout(1) ){
print(); continue;
}
}
}
printf("NO\n");
}
}