码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 5353 Average 模拟

时间:2015-08-08 22:55:03      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:


各种情况特判,然后枚举前两个点之间的关系

Average

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


/* ***********************************************
Author        :CKboss
Created Time  :2015年08月08日 星期六 09时35分46秒
File Name     :HDOJ5353.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;

const int maxn=100100;

int n;
int a[maxn],b[maxn],f[maxn];
LL s;

void print()
{
	puts("YES");
	int cnt=0;
	for(int i=0;i<n;i++)
		if(f[i]) cnt++;
	printf("%d\n",cnt);
	for(int i=0;i<n;i++)
	{
		int nx=i+1;
		if(nx==n) nx=0;
		if(f[i]==1)
		{
			printf("%d %d\n",i+1,nx+1);
		}
		else if(f[i]==-1)
		{
			printf("%d %d\n",nx+1,i+1);
		}
	}
}

bool func(int x)
{
	memcpy(b,a,sizeof(a));
	memset(f,0,sizeof(f));

	f[0]=x;
	b[0]-=x; b[1]+=x;
	b[n]=b[0];

	for(int i=1;i<n;i++)
	{
		if(abs(b[i])>=2) return false;
		if(b[i]==1)
		{
			f[i]=1;
			b[i]-=1;
			b[i+1]+=1;
		}
		else if(b[i]==-1)
		{
			f[i]=-1;
			b[i]+=1;
			b[i+1]-=1;
		}
	}
	return true;
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
    
	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d",&n);
		s=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d",a+i);
			s+=a[i];
		}
		if(s%n!=0) { puts("NO"); continue; }
		s=s/n;
		bool fg=true;
		bool zero=true;
		for(int i=0;i<n&&fg;i++)
		{
			a[i]-=s;
			if(a[i]!=0) zero=false; 
			if(!(a[i]>=-2&&a[i]<=2)) fg=false;
		}

		if(fg==false) { puts("NO"); continue; }
		if(zero==true) { puts("YES\n0"); continue; }

		if(func(0)) print();
		else if(func(1)) print();
		else if(func(-1)) print();
		else puts("NO");
	}

    return 0;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

HDOJ 5353 Average 模拟

标签:

原文地址:http://blog.csdn.net/ck_boss/article/details/47362643

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!