hdu5353

来源:互联网 发布:plc,单片机,dsp的区别 编辑:程序博客网 时间:2024/05/21 08:45

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
361 0 1 0 0 051 1 1 1 131 2 3
 

Sample Output
NOYES0YES22 13 2
 

Author
zimpha@zju
 

Source
2015 Multi-University Training Contest 6
 

Recommend

wange2014


题意:n个人,围城一个圈,每个人有一些糖果,每个人只能给他左边或右边那个人

一个糖果一次,问能否使所有人最后糖果数量相同

每个人只能传递一次,那么如果某个人有的糖果比平均值>2或<2肯定是无解的;

我们的做法是将每个人的糖果数减去平均值,那么这个数列就变成0 1 -1 2 -2 的序列了,包含3 -3 就是无解,当然所有数和要能够整除n是前提;

然后用一个二维数组保存状态 g[i][0]  g[i][1],g[1][0]表示第i个向左边给一个糖果

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

依次判断当前与右边项的传递关系,最后统计传递次数,输出g[][]数组


#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;}






0 0
原创粉丝点击