hdu 5353 Average(2015 Multi-University Training Contest 6)

Average

                                                                               Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
                                                                                                   Total Submission(s): 174    Accepted Submission(s): 27
                                                                                                                                    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
 

Source
2015 Multi-University Training Contest 6
 


题目大意:
      n个人形成的一个环,对于相邻的2个人(x,y),x可以给y一个糖果,y也可以给x一个糖果,也可以什么都不用做,判断是否可以让每个人的糖果相等,如果可以,并输出任意1种可行方案。

解题思路:
     首先,如果有人与平均值的差大于2,肯定不能构成,对于1个人来说,只能从左边要1个,和从右边要1个,也只能给左边一个和给右边1个。由于是1个环,处理的时候会比较麻烦,我们可以把它拆开,在0和n-1间拆开,于是就有 3种可能,n-1到0有可能是0,-1,1,分别枚举一下,我考虑的是与当前位置的后1个交换。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=100000+100;
int a[maxn];
struct node
{
    int x;
    int y;
};
vector<node> ve;
int main()
{
    int n,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        long long sum=0;
        int sign=1;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        if(sum%n==0)
        {
            int temp=sum/n;
            for(int i=0; i<n; i++)
            {
                a[i]=a[i]-temp;
                if(a[i]>2&&a[i]<-2)
                {
                    sign=0;
                    break;
                }
            }
            if(sign)
            {
                int cur=0,flag=1;;
                ve.clear();
                for(int i=0;i<n-1; i++)
                {
                    cur+=a[i];
                    if(cur<=-2||cur>=2)
                    {
                        flag=0;
                        break;
                    }
                    if(cur>0)
                    {
                        ve.push_back((node){i,i+1});
                    }
                    if(cur<0)
                    {
                        ve.push_back((node){i+1,i});
                    }
                }
                if(cur+a[n-1]!=0)
                    flag=0;
                if(!flag)
                {
                    cur=1,flag=1;
                    ve.clear();
                    ve.push_back((node){n-1,0});
                    for(int i=0; i<n-1; i++)
                    {
                        cur+=a[i];
                        if(cur<=-2||cur>=2)
                        {
                            flag=0;
                            break;
                        }
                        if(cur>0)
                        {
                            ve.push_back((node){i,i+1});
                        }
                        if(cur<0)
                        {
                            ve.push_back((node){i+1,i});
                        }
                    }
                    if(cur+a[n-1]!=1)
                        flag=0;
                    if(!flag)
                    {
                        cur=-1,flag=1;
                        ve.clear();
                        ve.push_back((node){0,n-1});
                        for(int i=0; i<n-1; i++)
                        {
                            cur+=a[i];
                            if(cur<=-2||cur>=2)
                            {
                                flag=0;
                                break;
                            }
                            if(cur>0)
                            {
                                ve.push_back((node){i,i+1});
                            }
                            if(cur<0)
                            {
                                ve.push_back((node){i+1,i});
                            }
                        }
                        if(cur+a[n-1]!=-1)
                            flag=0;
                        if(flag==0)
                            sign=0;
                    }
                }
            }
        }
        else
            sign=0;
        if(sign==0)
            printf("NO\n");
        else
        {
            printf("YES\n");
            printf("%d\n",ve.size());
            for(int i=0; i<ve.size();i++)
            {
                printf("%d %d\n",ve[i].x+1,ve[i].y+1);
            }
        }
    }
    return 0;
}


你可能感兴趣的:(Algorithm,编程,ACM,HDU,多校)