HDU 5353 Average(平分值,求步聚)多校6

Average

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1948    Accepted Submission(s): 495
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个人围成一圈,每个人手上都有糖果数量,每个人可以给相邻的人一个糖果且只能给一次,问能否使得每个人手上的糖果一样多,如果可以输出YES 并输出操作步聚,如果不行输出NO。
解题:分情况:avg:平均值。a[ i ] :第 i 个人手上的糖果
1.a[1]-avg==2:向左右两给一个糖果,然后从1->n进行判断操作。
2.a[1]-avg==-2:从左右各得到一个糖果,然后............................
3.a[1]-avg==1:(1):向第2个人给一个糖果,然后............。(2)向第n个人给一个糖果,然后…………。
4.a[1]-avg==-1:(1):从第2个人得到一个糖果,然后……。(2)从第n个人得到一个糖果,然后……。
5.a[1]-avg==0:(1)第1个什么都不做,然后.......……。(2)从第2个人过寄一个糖果给第1个人,再第1个人又给了第n个人,然后…………。(3)从第n个人过寄一个糖果给第1个人,再第1个人又给了第2个人,然后…………。
当然,如果再某个情况找到了就可以直接输出。

#include<stdio.h>
#include<string.h>
#define ll __int64
const int N  = 100005;
struct EDG{
    int x,y;
};
int n,step;
ll a[N],b[N],avg;
EDG edg[N<<1];
void addEdg(int x,int y)
{
    edg[step].x=x;
    edg[step].y=y;
    step++;
}
bool findout(int l)
{
    while(l<=n){
        if(a[l]==avg){l++;continue;}
        if(l==n)return 0;
        if(a[l]>avg){
            if(a[l]-avg>1){
                return 0;
            }
            else{
                l++;
                a[l]++;
                a[l-1]--;
                addEdg(l-1 , l); //从左向右过一个
            }
        }
        else {
            if(avg-a[l]>1){
                return 0;
            }
            if(a[l+1]<avg){
                return 0;
            }
            l++; a[l]--; a[l-1]++;  
            addEdg(l , l-1); //从右向左过一个
        }
    }

    return 1;
}
void print()
{
    printf("YES\n%d\n",step);
    for(int i=0; i<step; i++)
        printf("%d %d\n",edg[i].x,edg[i].y);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        avg=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%I64d",&b[i]);
            avg+=b[i] ;
        }
        if(avg%n!=0){
            printf("NO\n");continue;
        }
        avg/=n;
        step=0;
//因是一个圈,所以从第1 个分情况讨论,注意每次求完一轮把step 初始为0,我就是在这个WA了好多次
        if(b[1]-avg==2){
            for(int i=1; i<=n; i++)
            a[i]=b[i];
            a[1]--; a[2]++; addEdg(1 , 2);
            if(n>2){
                a[1]--;a[n]++; addEdg(1, n);
                if( findout(1) ){
                    print(); continue;
                }
            }
        }
        else if(b[1]-avg==-2){
            if(b[2]&&b[n]){
                for(int i=1; i<=n; i++)
                a[i]=b[i];
                a[1]++; a[2]--; addEdg(2 , 1);
                if(n>2){
                    a[1]++;a[n]--; addEdg(n, 1);
                    if( findout(1) ){
                        print(); continue;
                    }
                }
            }
        }
        else if(b[1]-avg==1){
            for(int i=1; i<=n; i++)
            a[i]=b[i];
            a[1]--; a[2]++; addEdg(1 , 2);
            if( findout(1) ){
                print(); continue;
            }
            step=0;
            for(int i=1; i<=n; i++)
            a[i]=b[i];
            a[1]--; a[n]++; addEdg(1 , n);
            if( findout(1) ){
                print(); continue;
            }
        }
        else if(b[1]-avg==-1){
            if(b[2]){
                for(int i=1; i<=n; i++)
                a[i]=b[i];
                a[1]++; a[2]--; addEdg(2 , 1);
                if( findout(1) ){
                    print(); continue;
                }
            }
            step=0;
            if(b[n]){
                for(int i=1; i<=n; i++)
                a[i]=b[i];
                a[1]++; a[n]--; addEdg(n , 1);
                if( findout(1) ){
                    print(); continue;
                }
            }
        }
        else if(avg==b[1]){
            for(int i=1; i<=n; i++)
            a[i]=b[i];
            if( findout(1) ){
                print(); continue;
            }
            step=0;
            if(b[2]){
                for(int i=1; i<=n; i++)
                a[i]=b[i];
                a[n]++; a[2]--; addEdg(1 , n); addEdg(2 , 1);
                if( findout(1) ){
                    print(); continue;
                }
            }
            step=0;
            if(b[n]){
                for(int i=1; i<=n; i++)
                a[i]=b[i];
                a[n]--; a[2]++;addEdg(1 , 2); addEdg(n , 1);
                if( findout(1) ){
                    print(); continue;
                }
            }
        }
        printf("NO\n");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值