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. Windows 下 Swoole开发环境配置

    一直停留在windows,入了 jetbrains 的全家桶.准备入门 Swoole,不可能每做一点修改就 git push 运行一下.因此要在 windows 上配置 swoole 运行环境.对比了 ...

  2. loj #2044. 「CQOI2016」手机号码

    #2044. 「CQOI2016」手机号码 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据   题目描述 ...

  3. Shell-3-文件之名

    1.生成任意大小的文件 [root@localhost tmp]# dd 记录了1+0 的读入 记录了1+0 的写出 1048576字节(1.0 MB)已复制,0.00219263 秒,478 MB/ ...

  4. Linux串口参数设置

    linux串口编程参数配置详解 1.linux串口编程需要的头文件 #include <stdio.h>         //标准输入输出定义#include <stdlib.h&g ...

  5. 2019.2.25考试T1, 矩阵快速幂加速递推+单位根反演(容斥)

    \(\color{#0066ff}{题解}\) 然后a,b,c通过矩阵加速即可 为什么1出现偶数次3没出现的贡献是上面画绿线的部分呢? 考虑暴力统计这部分贡献,答案为\(\begin{aligned} ...

  6. shiro 的简单应用

    shiro   的简单应用 shiro官网:https://shiro.apache.org/ shiro 简介: Apache Shiro(日语"堡垒(Castle)"的意思)是 ...

  7. 21. sessionStorage和localStorage的使用

    sessionStorage和localStorage的使用   前言 这是学习笔记,把从别人博客里转载的https://www.cnblogs.com/wangyue99599/p/9088904. ...

  8. kibana启动调试

    在windows的命令行窗口中,将路径制定到kibana的源代码bin目录中,然后执行 kibana.bat --dev --no-base-path 需要注意的是kibana.yml中的配置文件需要 ...

  9. input keyup的时候实现内容过滤

    当在文本框中输入关键字,就会搜索出包含关键字的数据 实现: 只需要一个内容过滤即可 <body> <input type="text" id="sear ...

  10. linux 的 sftp 和 scp

    ====================================== 作者: wxy0327(http://wxy0327.itpub.net) 发表于: 2006.12.07 13:19 分 ...