hdu5353

原创 2016年08月28日 17:10:46

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






版权声明:本文为博主原创文章,转载注明出处。 举报

相关文章推荐

hdu5353

这道题目因为围成的是一个圈,所以直接模拟的话情况会比较复杂,我们将环分割出来,成为一条直线,那么就比较好模拟。 怎么分割那?我们只要枚举0号和n-1号这两个人之间的交易即可。 由题目可知,有三种交...

hdu5353

题目名称:Average 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5353 题意:有n个soda围着坐在一张圆桌旁,第 i 个soda...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

HDU5353 Average

HDU5353 Average here are n soda sitting around a round table. soda are numbered from 1 to n and i-th...

hdu5353 Average

Problem Description There are n soda sitting around a round table. soda are numbered from 1 to ...

HDU5353:Average(贪心)

Problem Description There are n soda sitting around a round table. soda are numbered from 1 to ...

2015 多校联赛 ——HDU5353(构造)

Each soda has some candies in their hand. And they want to make the number of candies the same by do...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)