码迷,mamicode.com
首页 > 其他好文 > 详细

HDU5353:Average(贪心)

时间:2015-08-07 13:26:04      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:hdu   多校   

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


版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU5353:Average(贪心)

标签:hdu   多校   

原文地址:http://blog.csdn.net/libin56842/article/details/47336525

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!