Hdu 5353 Average (2015 Multi-University Training Contest 6), 5353 games

Source: Internet
Author: User

Hdu 5353 Average (2015 Multi-University Training Contest 6), 5353 games
AverageTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission (s): 174 Accepted Submission (s): 27
Special Judge


Problem DescriptionThere are N Soda sitting around a round table. soda are numbered from 1 To N And I -Th soda is adjacent (I + 1) -Th soda, 1 -St soda is adjacent 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
 
Source2015 Multi-University Training Contest 6

A ring formed by n people. For two adjacent people (x, y), x can give y a candy, and y can also give x a candy, you can also do nothing to determine whether or not each person's candy can be equal. If you can, you can output any one feasible solution.
Solution: first of all, if the difference between an average value and an average value is greater than 2, it cannot be formed. For one person, only one on the left and one on the right, you can only give one to the left and one to the right. Because it is a ring, it will be difficult to process it. We can split it between 0 and n-1, so there are three possibilities, n-1 to 0 may be 0,-, which are enumerated separately. What I want to consider is to exchange with the last one at the current position.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int maxn=100000+100;int a[maxn];struct node{    int x;    int y;};vector<node> ve;int main(){    int n,t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        long long sum=0;        int sign=1;        for(int i=0; i<n; i++)        {            scanf("%d",&a[i]);            sum+=a[i];        }        if(sum%n==0)        {            int temp=sum/n;            for(int i=0; i<n; i++)            {                a[i]=a[i]-temp;                if(a[i]>2&&a[i]<-2)                {                    sign=0;                    break;                }            }            if(sign)            {                int cur=0,flag=1;;                ve.clear();                for(int i=0;i<n-1; i++)                {                    cur+=a[i];                    if(cur<=-2||cur>=2)                    {                        flag=0;                        break;                    }                    if(cur>0)                    {                        ve.push_back((node){i,i+1});                    }                    if(cur<0)                    {                        ve.push_back((node){i+1,i});                    }                }                if(cur+a[n-1]!=0)                    flag=0;                if(!flag)                {                    cur=1,flag=1;                    ve.clear();                    ve.push_back((node){n-1,0});                    for(int i=0; i<n-1; i++)                    {                        cur+=a[i];                        if(cur<=-2||cur>=2)                        {                            flag=0;                            break;                        }                        if(cur>0)                        {                            ve.push_back((node){i,i+1});                        }                        if(cur<0)                        {                            ve.push_back((node){i+1,i});                        }                    }                    if(cur+a[n-1]!=1)                        flag=0;                    if(!flag)                    {                        cur=-1,flag=1;                        ve.clear();                        ve.push_back((node){0,n-1});                        for(int i=0; i<n-1; i++)                        {                            cur+=a[i];                            if(cur<=-2||cur>=2)                            {                                flag=0;                                break;                            }                            if(cur>0)                            {                                ve.push_back((node){i,i+1});                            }                            if(cur<0)                            {                                ve.push_back((node){i+1,i});                            }                        }                        if(cur+a[n-1]!=-1)                            flag=0;                        if(flag==0)                            sign=0;                    }                }            }        }        else            sign=0;        if(sign==0)            printf("NO\n");        else        {            printf("YES\n");            printf("%d\n",ve.size());            for(int i=0; i<ve.size();i++)            {                printf("%d %d\n",ve[i].x+1,ve[i].y+1);            }        }    }    return 0;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.