Average

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2992    Accepted Submission(s): 720
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.

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 (1n105), the number of soda.
The next line contains n integers a1,a2,,an (0ai109), 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(0mn) in the second line denoting the number of operations needed. Then each of the following m lines contain two integers x and y (1x,yn), 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

Author
zimpha@zju

Source

Recommend

wange2014

g[i][1]表示向右边给一个糖果，然后用一个0-2*n的循环表示n个人围成的圈

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;
typedef long long LL;
const int maxn=1e5+10;
int g[maxn][2];
int a[maxn];
int main()
{
int t,n;
int x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
LL sum=0;
memset(g,0,sizeof(g));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
//cout<<"ff"<<endl;
if(sum%n)
{
puts("NO");
continue;
}
sum/=n;
int flag=1;
for(int i=0;i<n;i++)
{
a[i]-=sum;
if(a[i]>2)
flag=0;
}
// cout<<"dd"<<endl;
for(int i=0;i<2*n;i++)
{
x=i%n;
y=(i+1)%n;
if(a[x]>0&&a[y]<=0&&!g[x][1])///x没有向右给予
{
a[x]--;
a[y]++;
if(!g[y][0])///y也没有向左给予
g[x][1]=1;
else g[x][1]=g[y][0]=0;///y向左给予，两者抵消
}
else if(a[x]<0&&a[y]>=0&&!g[y][0])///类似上面
{
a[x]++;
a[y]--;
if(!g[x][1])
g[y][0]=1;
else
g[x][1]=g[y][0]=0;
}
}

int temp=0;
for(int i=0;i<n;i++)
{
if(a[i])///最终结果a全部变成0
{
flag=0;
break;
}
temp+=g[i][0]+g[i][1];
}
if(flag)
{
printf("YES\n%d\n",temp);
for(int i=1;i<=n;i++)
{
x=i-1;
y=i+1;
if(x==0)
x=n;
if(y==n+1)
y=1;
if(g[i-1][0])
printf("%d %d\n",i,x);
if(g[i-1][1])
printf("%d %d\n",i,y);
}
}
else
{
puts("NO");
}
}
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：hdu5353 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)