HDU 5363 Average(贪心+脑洞)

Average

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

Author
zimpha@zju
 

Source
2015 Multi-University Training Contest 6
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5994  5993  5992  5991  5990 
 


题目大意:

    有N过杯子,每一个有ai个糖果,每相邻两个之间可以最多传递一个糖果,问能不能最后每个杯子的糖果一样多,并出处传递过程。


解题思路:

    这题的最基本思路和之前CF的一道题很像,就是把不正常的状态按照一个方向传递给相邻的转态,使得这个状态正常。

    首先,如果糖果总数不能被等分的话就一定错误,其次每个杯子最多给出或接受两个糖果,如果其糖果数与平均值差的绝对值超过2也一定不可以。

    由于杯子形成了一个圈,所以糖果的传递一共只有两种转态,向前穿一个和向后传一个。我们就可以使用贪心的思路从左到右扫,每次尽量使得当前被子达到要求。这样一共三种情况,如果本来就达到要求了就跳过;如果多了就向右传递一个,如果少了就让右边的传来一个。

    由于是一个环,所以我们还需要扫第二次,重复刚才的操作(注意可能会出现类似回溯的情况,要把上次的操作抵消掉)。这样我们得到的结果就一定是正确的。最后判断一下经过操作有没有达到要求,输出即可。


AC代码:

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define mem(a,b) memset((a),(b),sizeof(a))

const int MAXN=100000+3;
LL N,cup[MAXN];
bool l[MAXN],r[MAXN];//是否左移、右移

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld",&N);
        bool ok=true;
        LL ave=0;
        for(int i=0;iave+2||cup[i]ave&&r[i]==false&&cup[(i+1)%N]<=ave)//右移
            {
                --cup[i];
                ++cup[(i+1)%N];
                if(l[(i+1)%N]==true)//和上次操作相抵消
                    l[(i+1)%N]=false;
                else r[i]=true;
            }
            else if(cup[i]=ave)//左移
            {
                ++cup[i];
                --cup[(i+1)%N];
                if(r[i]==true)//和上次操作相抵消
                    r[i]=false;
                else l[(i+1)%N]=true;
            }
        }
        //第二轮
        for(int i=0;iave&&r[i]==false&&cup[(i+1)%N]<=ave)//右移
            {
                --cup[i];
                ++cup[(i+1)%N];
                if(l[(i+1)%N]==true)
                    l[(i+1)%N]=false;
                else r[i]=true;
            }
            else if(cup[i]=ave)//左移
            {
                ++cup[i];
                --cup[(i+1)%N];
                if(r[i]==true)
                    r[i]=false;
                else l[(i+1)%N]=true;
            }
        }
        
        LL num=0;
        for(int i=0;i


你可能感兴趣的:(算法,贪心,脑洞题,HDU)