zoukankan      html  css  js  c++  java
  • HDU 5353 Average

    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

    这题要注意可能会有从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
    %d
    ",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
    ",i,j);
                    if (f[i-1][1]) printf("%d %d
    ",i,k);
                 }
            }
            else printf("NO
    ");
        }
        return 0;
    }


  • 相关阅读:
    NOI2013
    【FINAL】NOI
    【jsoi】第一季 [略]精简题解
    浏览器缓存机制--小总结
    UC和QQ两个主流浏览器 * 点击触发微信分享到朋友圈或发送给朋友的功能(转载)
    webpack加载postcss,以及autoprefixer的loader
    HTTP协议中的短轮询、长轮询、长连接和短连接,看到一篇文章有感
    webpack2.0 css文件引入错误解决及图片输出在根目录配置问题
    webpack+vue 我的视角(持续更新)
    手机端图像编辑上传-cropper
  • 原文地址:https://www.cnblogs.com/jhcelue/p/6937510.html
Copyright © 2011-2022 走看看