Solved Pro.ID Title Author Source (AC/Submit)Ratio
  5353 Average   2015 Multi-University Training Contest 6 (433/1704)25.41%
  5354 Bipartite Graph   2015 Multi-University Training Contest 6 (29/137)21.17%
  5355 Cake   2015 Multi-University Training Contest 6 (301/1709)17.61%
  5356 Deal   2015 Multi-University Training Contest 6 (16/43)37.21%
  5357 Easy Sequence   2015 Multi-University Training Contest 6 (75/285)26.32%
  5358 First One   2015 Multi-University Training Contest 6 (428/1406)30.44%
  5359 Group   2015 Multi-University Training Contest 6 (1/1)100.00%
  5360 Hiking   2015 Multi-University Training Contest 6 (443/848)52.24%
  5361 In Touch   2015 Multi-University Training Contest 6 (247/919)26.88%
  5362 Just A String   2015 Multi-University Training Contest 6 (93/385)24.16%
  5363 Key Set   2015 Multi-University Training Contest 6 (401/651)61.60%

 

HDU5353

n个人成环,每个人有a[i]块糖

相邻两个人x,y之间有且只有其中一种操作:

1 x给y一颗糖

2 y给x一颗糖

3 do nothing

求能否使得每个人手中糖的数量一样,如果可以任意一种输出步骤,spj

只要前两人的操作定下来后,后面的人的操作就定下来了,因为每条边只能有一种状态。3个for循环搞定。O(n)

#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
#define maxn 100010
int n;
int s[maxn],a[maxn];
int avg;
int cnt;
bool run()
{
    int i=2;
    int flag=1;
    while(i!=2||flag)
    {
        flag=0;

        int pre=(i-1+n)%n;
        if(a[pre]==avg);
        else if(a[pre]==avg+1)
        {
            a[pre]--;
            a[i]++;
            cnt++;
        }
        else if(a[pre]==avg-1)
        {
            a[pre]++;
            a[i]--;
            cnt++;
        }
        else return false;

        i=(i+1)%n;
    }
    return true;
}
void print()
{
    int i=2;
    int flag=1;
    while(i!=2||flag)
    {
        flag=0;
        int pre=(i-1+n)%n;
        if(s[pre]==avg);
        else if(s[pre]==avg+1)
        {
            s[pre]--;
            s[i]++;
            printf("%d %d\n",pre+1,i+1);
        }
        else if(s[pre]==avg-1)
        {
            s[pre]++;
            s[i]--;
            printf("%d %d\n",i+1,pre+1);
        }
        i=(i+1)%n;
    }
    return ;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        ll sum=0;
        for(int i=0;i<n;i++){
            scanf("%d",&s[i]);
            sum+=s[i];
        }
        avg=sum/n;
        if(n==1)
        {
            printf("YES\n0\n");
            continue;
        }
        if(sum%n!=0)
        {
            printf("NO\n");
            continue;
        }
        //0--1
        cnt=0;
        for(int i=0;i<n;i++)a[i]=s[i];
        if(run())
        {
            printf("YES\n%d\n",cnt);
            print();
            continue;
        }

        //0->1
        cnt=1;
        for(int i=0;i<n;i++)a[i]=s[i];
        a[0]++;
        a[1]--;
        if(run())
        {
            s[0]++;
            s[1]--;
            printf("YES\n%d\n2 1\n",cnt);
            print();
            continue;
        }
        //0<-1;
        cnt=1;
        for(int i=0;i<n;i++)a[i]=s[i];
        a[0]--;
        a[1]++;
        if(run())
        {
            s[0]--;
            s[1]++;
            printf("YES\n%d\n1 2\n",cnt);
            print();
            continue;
        }
        puts("NO");
    }
    return 0;
}
View Code

 

posted on 2015-08-08 20:43  kylehz  阅读(187)  评论(0编辑  收藏  举报