hdu 5353 Average(2015 Multi-University Training Contest 6),5353小遊戲

來源:互聯網
上載者:User

hdu 5353 Average(2015 Multi-University Training Contest 6),5353小遊戲
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 DescriptionThere 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. 
InputThere 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.
 
OutputFor 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

361 0 1 0 0 051 1 1 1 131 2 3
 
Sample Output
NOYES0YES22 13 2
 
Source2015 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;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.