HDU 5353 Average,hdu5353average

來源:互聯網
上載者:User

HDU 5353 Average,hdu5353average
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

這題要注意可能會有從n繞回1的,或者從1繞到n的,感覺情況比較多。。必須注意2的情況。。

#include<cstdio>#include<cmath>#include<iostream>#include<queue>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const ll maxn=100005;ll T,n,m,a[maxn],sum,tot;bool flag;int f[maxn][2];int main(){    scanf("%lld",&T);    while (T--)    {        scanf("%lld",&n);        sum=0;    flag=true;        for (int i=0;i<n;i++)         {            scanf("%lld",&a[i]);            sum+=a[i];        }        if (sum%n) flag=false;        else         {            sum=sum/n;            tot=0;            for (int i=0;i<n;i++) a[i]-=sum;            memset(f,0,sizeof(f));            for (int i=0,j,k;i<n+n;i++)            {                j=i%n;    k=(i+1)%n;                if (a[j]>0&&a[k]<=0&&!f[j][1]) {                     a[k]++;                    a[j]--;                    if (!f[k][0]) f[j][1]=1;                      else f[k][0]=f[j][1]=0;                 }                else if (a[j]<0&&a[k]>=0&&!f[k][0]) {                      a[k]--;                    a[j]++;                    if (!f[j][1]) f[k][0]=1;                      else f[j][1]=f[k][0]=0;                 }            }            for (int i=0;i<n;i++) if (a[i]) {flag=false; break;}             for (int i=0;i<n;i++) tot+=f[i][0]+f[i][1];            if (n==2&&tot==2) flag=false;        }        if (flag)        {            printf("YES\n%d\n",tot);            for (int i=1,j,k;i<=n;i++)             {                j=i-1;    if (j==0) j=n;                k=i+1;  if (k>n) k=1;                if (f[i-1][0]) printf("%d %d\n",i,j);                if (f[i-1][1]) printf("%d %d\n",i,k);             }        }        else printf("NO\n");    }    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.