hdu5353 (贪心)

题意:
有n个人组成一个环,相邻的两个人能互相给糖果,对于相邻的两个人而言,只能进行一次操作,要么x给y一个糖果,要么y给x一个糖果,要么不动,问能否经过一定的操作使得每个人的糖果数一样,并输出步骤
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≤105), the number of soda.
The next line contains n integers a1,a2,…,an (0≤ai≤109), 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 (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

思路:可以从第一个点出发,先向右贪心,由于刚开始第一个点左边的一个点没做考虑,然后又左贪心,这样所有情况就考虑了;我们可以优化从第一点开始直到第n*2个点;每个点i优化成i%n;等于循环两次,这样所有情况就考虑了,不必左循环然后右循环;
代码:

#include<stdio.h>
#include<string.h>
#define N 100005
__int64 a[N];
int b[N][2];//f[i][0]=1代表向前移动一个;f[i][1]=1代表向后移动一个,
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,i,j;
        __int64 sum=0;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%I64d",&a[i]);
            sum=sum+a[i];
        }
        if(sum%n)
        {
            printf("NO\n");
            continue;
        }
        sum=sum/n;
        for(i=0;i<n;i++)
            a[i]=a[i]-sum;
        memset(b,0,sizeof(b));
        for(i=0;i<2*n;i++)
        {
            int x=i%n;
            int y=(i+1)%n;
            if(a[x]>0&&a[y]<=0&&!b[x][1])//x向下个点转换一
            {
                a[x]--;
                a[y]++;
                if(!b[y][0]) b[x][1]=1;//若果后一点没有向前转换一,则x可以向后转换一
                else
                    b[y][0]=0; //如果后一点 向前转换了一,前一点向后转换一,则等于没有做变化,抵消了;
            }
            else if(a[x]<0&&a[y]>=0&&!b[y][0])
            {
                a[x]++;
                a[y]--;
                if(!b[x][1]) b[y][0]=1;
                else
                    b[x][1]=0;
            }
        }
        int flag=1,cnt=0;
        for(i=0;i<n;i++)//判断每个点是否都为平均值,然后记录转换的次数;
        {
            if(a[i])
            {
                flag=0;
                break;
            }
            cnt=cnt+b[i][0]+b[i][1];
        }
        if(flag)
        {
            printf("YES\n%d\n",cnt);
            for(i = 1; i<=n; i++)
            {
                int x = i-1;
                int y = i+1;
                if(x == 0)
                    x = n;
                if(y == n+1)
                    y = 1;
                if(b[i-1][0]) printf("%d %d\n",i,x);
                if(b[i-1][1]) printf("%d %d\n",i,y);
            }
        }
        else
            printf("NO\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值