Average

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

 
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≤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
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
 

题目大意:有n个人围成一圈,每个人手上都有一定数量的糖果,相邻两个人可是给一个糖果,但是只能给一次,如x,y。x可以给y一个糖果,或者y可以给x一个糖果。也可以不操作,即都不给糖果。第一个人可以给第n个人一个糖果,或者第n个人给第一个人一个糖果,或不给。问是否可以通过给糖果这样的操作,让所有的人手中的糖果数量相同。如果可以,输出YES和需要多少次,同时输出路径。否则输出NO。

解题思路:枚举第一个人对第二个人的三种操作,然后看第二个人跟average的差值,如果是-1,那么第二个人从第三个人手中拿走一个糖果,如果是1,那么第二个人给第三个人一个糖果,如果为零,则不操作。依次遍历到n。因为第n个人跟第1个人是可以操作的,所以我在操作第n个的时候,把对第一个人的操作放在了n+1的位置,c[1]+=c[n+1]判断第一个人的结果是否为零,如果为零,这说明可以,否则不可以。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn=1e5+200;
typedef long long INT;
int a[maxn] ,b[maxn] ,c[maxn];
int f[3]={0,1,-1};
int abs(int x){
return x>0? x: -x;
}
struct Oper{
int x,y;
}oper[maxn];
int main(){ int t,n;
scanf("%d",&t);
while(t--){ scanf("%d",&n);
INT sum=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
if(sum%n!=0){
printf("NO\n");continue;
}else{
if(n==1){ //1个的时候必然YES
printf("YES\n0\n");
continue;
}
if(n==2){ //特判两个的情况
if(a[1]-a[2]==2){
printf("YES\n1\n");
printf("1 2\n"); continue;
}else if(a[1]-a[2]==-2){
printf("YES\n1\n");
printf("2 1\n");continue;
}else if(a[1]==a[2]){
printf("YES\n");
printf("0\n");continue;
}
}
const int ave=sum/n;
int flag=0;
for(int i=1;i<=n;i++){
b[i]=a[i]-ave; //把跟平均数的差值放在b数组中
if(abs(b[i])>2){
printf("NO\n");
flag=1;
break;
}
}
if(flag)
continue;
int num;
b[n+1]=0;
for(int k=0;k<3;k++){
num=0; //num放错位置,wa了
for(int i=1;i<=n+1;i++){
c[i]=b[i];
}
if(f[k]==1){
oper[num].x=2;
oper[num++].y=1;
}else if(f[k]== -1){
oper[num].x=1;
oper[num++].y=2;
}
//每次对c数组操作
c[1]+=f[k];
if(abs(c[1])>1 ){
continue;
}
c[2]-=f[k];
if(abs(c[2])>1){
continue;
}
int mark=0;
for(int i=2;i<=n;i++){ //遍历2-->n
if(c[i]==1){
c[i]-=1;
c[i+1]+=1;
if(i<n){
oper[num].x=i;
oper[num++].y=i+1;
}else{
oper[num].x=n;
oper[num++].y=1;
} }else if(c[i]==-1){
c[i]+=1;
c[i+1]-=1;
if(i<n){
oper[num].x=i+1;
oper[num++].y=i;
}else{
oper[num].x=1;
oper[num++].y=n;
}
}
else if(abs(c[i])>1){
mark=1;
break;
}
}
if(mark){
continue;
}
if(c[1]+c[n+1]!=0){
continue;
}else{
flag=1;
break;
}
}
if(flag){
printf("YES\n");
printf("%d\n",num);
for(int i=0;i<num;i++){
printf("%d %d\n",oper[i].x,oper[i].y);
}
}else{
printf("NO\n");
} } }
return 0;
}

  

HDU 5353—— Average——————【贪心+枚举】的更多相关文章

  1. 2015多校第6场 HDU 5353 Average 贪心,细节处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5353 题意:有n个人围城一个环,每一个人手里都有一些糖果,第i个人有ai块.现在有三种操作:第i个人给 ...

  2. HDU 5353 Average 贪心

    就是贪心啊,不知道为啥总是不过,总是WA 方法不对吗? 将数组扩展一倍,从左到右扫描,大于平均数就给右边的,小于就从右边拿,等于就不变,记录下操作类型. 大于2直接NO,不知道哪错了,自己出了一些数据 ...

  3. HDU 5353 Average 糖果分配(模拟,图)

    题意:有n个人坐在圆桌上,每个人带着糖果若干,每次只能给旁边的人1科糖果,而且坐相邻的两个人最多只能给一次(要么你给我,要么我给你),问是否能将糖果平均分了. 思路: 明显每个人最多只能多于平均值2个 ...

  4. HDU 5353 Average

    Problem Description There are n soda sitting around a round table. soda are numbered from 1 to n and ...

  5. 思维/构造 HDOJ 5353 Average

    题目传送门 /* 思维/构造:赛后补的,当时觉得3题可以交差了,没想到这题也是可以做的.一看到这题就想到了UVA_11300(求最小交换数) 这题是简化版,只要判断行不行和行的方案就可以了,做法是枚举 ...

  6. Hdu 4864(Task 贪心)(Java实现)

    Hdu 4864(Task 贪心) 原题链接 题意:给定n台机器和m个任务,任务和机器都有工作时间值和工作等级值,一个机器只能执行一个任务,且执行任务的条件位机器的两个值都大于等于任务的值,每完成一个 ...

  7. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

  8. D - 淡黄的长裙 HDU - 4221(贪心)

    D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...

  9. HDU 5303 Delicious Apples (贪心 枚举 好题)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

随机推荐

  1. 验证视图状态MAC失败的解决办法

    在网上搜寻了很久看了很多关于MAC验证视图状态失败的解决方法.大部分人都说是在页里或web.config里加 EnableEventValidation="false" Enabl ...

  2. Matplotlib中plot画点图和折线图

    引入: import matplotlib.pyplot as plt 基本语法: plt.plot(x, y, format_string, **kwargs) x:x轴数据,列表或数组,可选 y: ...

  3. 读《JavaScript权威指南》笔记(五)

    1.getComputedStyle()方法的返回值是一个CSSStyleDeclaration对象,它代表了应用在指定元素(或伪对象)上的所有样式. 2.clip style="clip: ...

  4. android: 获取屏幕高度和虚拟导航栏高度的几种方法

    package com.yongdaimi.android.androidapitest; import android.app.Activity; import android.content.Co ...

  5. Serialization之BinaryFormatter

    前言 BinaryFormatter序列化二进制序列化使用二进制编码来生成精简的序列化,以用于存储或基于套接字的网络流等. 内容 下面通过一个小小的例子来给大家说明什么是BinaryFormatter ...

  6. ReentranLock实现原理

    原文链接:https://blog.csdn.net/jeffleo/article/details/56677425 一.ReentranLock 相信我们都使用过ReentranLock,Reen ...

  7. Python翻译器

    import urllib.request import urllib.parse import json content='有了我,翻译再也不是问题啦' print(content) while c ...

  8. HDU6315 Naive Operations(多校第二场1007)(线段树)

    Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Other ...

  9. wordpress显示FTP上传

    在开始添加的配置文件里添加以下内容 define("FS_METHOD","direct"); define("FS_CHMOD_DIR", ...

  10. 11. 变量提升 && 执行上下文

    /* 变量升级 预处理 */ /* js引擎在代码正式执行之前会做一个预处理的工作: 1.收集变量 2.收集函数 依据: var 将var后边的变量定义但不赋值 var username=undefi ...