HDU 5353(Average-贪心分果)

Average

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 2153    Accepted Submission(s): 532

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 \le n \le 10^5)$, the number of soda.

The next line contains $n$ integers $a_1, a_2, \dots, a_n$ $(0 \le a_i \le 10^9)$, where $a_i$ 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 \le m \le 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 \le x, y \le 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

Source

2015 Multi-University Training Contest 6

Recommend

wange2014

首先一个人有三种操作,每个人选择的操作影响周边2人.

如果无环的化可直接贪心。

所以枚举人1和人2的关系,把环从1与2之间断开

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define mp make_pair
#define MAXN (100000+10)
typedef long long ll;
int n;
int a[MAXN],a2[MAXN];
void rel(int &a,int &b,int c)
{
	a=c-1,b=c+1;
	if (a==0) a=n;if (b>n) b=1;
}
pair<int,int>ans[MAXN];
int siz=0;
bool check(int a[])
{
	a[n+1]=a[1];
	Fork(i,2,n)
	{
		if (a[i]==-1) a[i]++,a[i+1]--,ans[siz++]=mp(i+1<=n?i+1:1,i);
		else if (a[i]==1) a[i]--,a[i+1]++,ans[siz++]=mp(i,i+1<=n?i+1:1);
	}
	a[1]=a[n+1];
	For(i,n) if (a[i]) return 0;
	return 1;
}
void pri()
{
	cout<<"YES\n"<<siz<<'\n';
	Rep(i,siz) printf("%d %d\n",ans[i].first,ans[i].second);
}
int main()
{
//	freopen("A.in","r",stdin);

	int T;
	cin>>T;
	For(kcase , T)
	{
		cin>>n;
		For(i,n) scanf("%d",&a[i]);
		ll sum=0;
		For(i,n) sum+=a[i];
		if (sum%n){
			cout<<"NO\n";continue;
		}
		sum/=n;
		For(i,n) a[i]-=sum;

		bool flag=0;
		For(i,n) if (a[i]<-2||a[i]>2){
			cout<<"NO\n"; flag=1;break;
		}
		if (flag==1) continue;

		memcpy(a2,a,sizeof(a));
		MEM(ans) siz=0;
		if (check(a2))
		{
			pri();
		 	continue;
		} 

		memcpy(a2,a,sizeof(a));
		MEM(ans) siz=0;
		a2[1]--;a2[2]++;ans[siz++]=mp(1,2);
		if (check(a2))
		{
			pri();
		 	continue;
		} 

		memcpy(a2,a,sizeof(a));
		MEM(ans) siz=0;
		a2[1]++;a2[2]--;ans[siz++]=mp(2,1);
		if (check(a2))
		{
			pri();
		 	continue;
		} 

		cout<<"NO\n";
	}

	return 0;
}

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

时间: 2024-08-11 01:36:26

HDU 5353(Average-贪心分果)的相关文章

HDU 5353 Average 贪心

就是贪心啊,不知道为啥总是不过,总是WA 方法不对吗? 将数组扩展一倍,从左到右扫描,大于平均数就给右边的,小于就从右边拿,等于就不变,记录下操作类型. 大于2直接NO,应该是正确的算法吧,自己出了一些数据也都过了 1 #pragma comment(linker, "/STACK:102400000,102400000") 2 #include <iostream> 3 #include <cstdio> 4 #include <fstream>

HDU 5353—— Average——————【贪心+枚举】

Average Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2069    Accepted Submission(s): 517Special Judge Problem Description There are n soda sitting around a round table. soda are numbered fr

HDU 5353 Average(平分值,求步聚)多校6

Average Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1948    Accepted Submission(s): 495 Special Judge Problem Description There are n soda sitting around a round table. soda are numbered

HDU 5353 Average

题意:有n个人坐在圆桌上,每个人带着糖果若干,每次只能给旁边的人1科糖果,而且坐相邻的两个人最多只能给一次(要么你给我,要么我给你),问是否能将糖果平均分了. 思路:明显每个人最多只能多于平均值2个糖果,因为他只能分别往左和右边的人给1颗.而多于平均值1的人可以任意选1个方向,只要到最后所有人满足了即可.多余糖果超过3的.平均数是浮点型的都是无解. 在第i和第i+1个人之间建两条边(即无向边拆成2条有向边),分别从一方指向另一方.1和n也建两条.分两步: (1)将持有2个多余糖果的人先处理,用D

hdu 4869 Task(贪心)

题目链接:hdu 4869 Task 题目大意:有n台机器,m个任务,每个机器和任务都有有xi和yi,要求机器的xi,yi均大于等于任务的xi和yi才能执行任务.每台机器一天只能执行一个任务.要求完成的任务数尽量多,并且说金额尽量大.完成每个任务的金额为xi?500+yi?2 解题思路:贪心,mach[i][j]表示等级为i,时间为j的机器数量,task[i][j]表示等级为i,时间为j的机器数量.每次优先减少i,因为对应等级减少100,对应的金额代价也不会减少超过500(即时间减少1). 每次

HDU 4864 Task(贪心)

HDU 4864 Task 题目链接 题意:有一些机器和一些任务,都有时间和等级,机器能做任务的条件为时间等级都大于等于任务,并且一个任务只能被一个机器做,现在求最大能完成任务,并且保证金钱尽量多 思路:贪心,对于每个任务,时间大的优先去匹配,时间相同的,等级大的优先去匹配,因为时间占得多,时间多1就多500,而等级最多才差200.然后匹配的时候,尽量使用等级小的去匹配,而时间只要大于它的都可以用,因为是按时间优先,所以如果该时间能匹配大的,其他肯定也能匹配,那么肯定优先匹配大的,所以只要在等级

HDU 4811 Ball(贪心)

2014-05-15 22:02 by Jeff Li 前言 系列文章:[传送门] 马上快要期末考试了,为了学点什么.就准备这系列的博客,记录复习的成果. 正文-计数  概率 概率论研究随机事件.它源于赌徒的研究.即使是今天,概率论也常用于赌博.随机事件的结果是否只凭运气呢?高明的赌徒发现了赌博中的规律.尽管我无法预知事件的具体结果,但我可以了解每种结果出现的可能性.这是概率论的核心. "概率"到底是什么?这在数学上还有争议."频率派"认为概率是重复尝试多次,某种结

HDU 4912 LCA+贪心

Paths on the tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 531    Accepted Submission(s): 182 Problem Description bobo has a tree, whose vertices are conveniently labeled by 1,2,…,n. Th

HDU 1661 Assigments 贪心法题解

Problem Description In a factory, there are N workers to finish two types of tasks (A and B). Each type has N tasks. Each task of type A needs xi time to finish, and each task of type B needs yj time to finish, now, you, as the boss of the factory, n