HDU 5353 Average(贪心)

Average

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3842    Accepted Submission(s): 888
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 (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.

 

 

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 (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

 

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~n围成一圈,每个人手里都有一定的糖果数目,每相邻的两个人必须选择以下三种操作之一:

1、第i个人给第i+1个人一颗糖。

2、第i+1个人给第i个人一颗糖。

3、第i个人和第i+1个人不进行任何操作。

让你求能否让所有人通过操作后糖果数目相等。如果能,输出YES并输出给糖的操作数,和每个操作数谁给了谁一颗糖。

如果不能,输出NO。

思路:贪心。如果想枚举一个人给左边还是给右边还是不变那就太麻烦了。而且不可行(因为我WA了若干发)。

考虑到每相邻的两个人只有三种操作,因此我们只需要枚举第一个人和第二个人的三种操作,然后后面少于平均值的从后面的人拿糖果,多于平均值的人把糖果给后面的人。

注意:所有人的初始糖果数如果是相等的,就一定是YES和0。如果糖果总数不能整除n就是NO。如果每个人的糖果数-平均糖果数>2,则一定是NO。(刚开始写成了1,结果不对,因为一个人如果多于平均值2,他可以既给左边又给右边)

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=200010;
int n,m,k,q;
ll a[maxn],c[maxn];
ll ans,cnt,tmp,flag;
int z[maxn],y[maxn],f;
bool go(int st)
{
    for(int i=st;i<n;i++)
    {
        if(c[i]==1){c[(i+1)%n]++;c[i]--;z[f]=i+1;y[f++]=(i+1)%n+1;}
        else if(c[i]==-1) {c[(i+1)%n]--;c[i]++;z[f]=(i+1)%n+1;y[f++]=i+1;}
        else if(c[i])return 0;
    }
    if(c[0])return 0;
    return 1;
}
void solve()
{
    puts("YES");
    printf("%d\n",f);
    for(int i=0;i<f;i++)
    printf("%d %d\n",z[i],y[i]);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        tmp=0;  flag=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
            tmp+=a[i];
            if(i&&a[i]!=a[i-1]) flag=1;
        }
        if(tmp%n){puts("NO");continue;}
        if(!flag){puts("YES");puts("0");continue;}
        tmp/=n;
        flag=0;
        for(int i=0;i<n;i++) {a[i]-=tmp;
        if(a[i]>2||a[i]<-2) flag=1;
        }
        if(flag){puts("NO");continue;}
        memcpy(c,a,sizeof(a));
        f=0;
        if(go(1))solve();
        else
        {
            memcpy(c,a,sizeof(a));
            c[0]++;c[1]--;f=0;
            z[f]=2;y[f++]=1;
            if(go(1))solve();
            else
            {
            memcpy(c,a,sizeof(a));
            c[0]--;c[1]++;f=0;
            z[f]=1;y[f++]=2;
            if(go(1))solve();
            else puts("NO");
            }
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值