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
 


題目大意:
      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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章