HDU5353:Average(贪心)

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
 

Source


题意:
有n个人组成一个环,相邻的两个人能互相给糖果,对于相邻的两个人而言,只能进行一次操作,要么x给y一个糖果,要么y给x一个糖果,要么不动,问能否经过一定的操作使得每个人的糖果数一样,并输出步骤

思路:
一开始以为对于相邻的两个人能操作多次,错了好久。
对于成环的问题,我们常用的方法就是复制一个数组接在后面。


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define ls 2*i
#define rs 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 100005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define rank rank1
const int mod = 1000000007;

int t,n;
int a[N],f[N][2];//f[i][0]=1代表向前移动一个,f[i][1]=1代表向后移动一个,
LL sum;

int main()
{
    int i,j,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        sum = 0;
        MEM(f,0);
        for(i = 0; i<n; i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        if(sum%n)
        {
            printf("NO\n");
            continue;
        }
        sum/=n;
        for(i = 0; i<n; i++)
            a[i]-=sum;
        for(i = 0; i<2*n; i++)
        {
            int x = i%n;
            int y = (i+1)%n;
            if(a[x]>0&&a[y]<=0&&!f[x][1])//x向后移动一个
            {
                a[x]--;
                a[y]++;
                if(!f[y][0]) f[x][1] = 1;//y并没有向前移则可以向后移
                else f[x][1] = f[y][0] = 0;//否则两两抵消
            }
           else if(a[y]>=0&&a[x]<0&&!f[y][0])
            {
                a[x]++;
                a[y]--;
                if(!f[x][1]) f[y][0] = 1;
                else f[x][1] = f[y][0] = 0;
            }
        }
        int flag = 1,cnt = 0;
        for(i = 0; i<n; i++)
        {
            if(a[i])
            {
                flag = 0;
                break;
            }
            cnt+=f[i][0]+f[i][1];
        }
        if(n==2&&cnt==2)
            flag = 0;
        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(f[i-1][0]) printf("%d %d\n",i,x);
                if(f[i-1][1]) printf("%d %d\n",i,y);
            }
        }
        else
            printf("NO\n");
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值