猿的进化

ACM~java~web~密码学

hdu5353

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


题意: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;
}






阅读更多
版权声明:本文为博主原创文章,转载注明出处。 https://blog.csdn.net/yuanjunlai141/article/details/52346584
个人分类: -------贪心
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

hdu5353

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭