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. spark(3) - scala独立编程

    >>非集成: 环境需要 * spark 2.4.0 * scala 2.11.12 * sbt (打包) 开发过程 1.shell命令下创建项目目录结构 *****/ project / ...

  2. day03-Linux操作系统目录结构

    一. Linux系统目录表示        ‘/’表示根目录:                                                            ‘.’表示当前目录 ...

  3. poj3167(kmp)

    题目链接: http://poj.org/problem?id=3167 题意: 给出两串数字 s1, s2, 求主串 s1 中的 s2 匹配数并输出每个匹配的开头位置. 区间 [l, r] 是 s2 ...

  4. oracle修改连接数

    使用 sqlplus登陆   sqlplus system 然后切换到sysdba模式   conn ?/ as sysdba   查询当前的processes sessions的大小   show ...

  5. CI框架源码学习笔记4——Benchmark.php

    我们回到Codeigniter.php上继续往下看,第一个引入的类文件是Benchmark.php,这个文件主要是提供基准测试,具体使用方法参考手册http://codeigniter.org.cn/ ...

  6. luogu1210 回文检测

    Manacher 正确读法:抹内A撤(马拉车) (跟着假硕学英语) 我们把原来的字符串,通过玄学处理,变成只留下字母,且每两个字母之间有一个奇怪的字符的那种Manacher专用字符串. 建立双射关系f ...

  7. There is no row in position 0

    更改程序池 管道模式 ---->经典    常见设置问题: 32位启用

  8. tinkphp中的自动验证

    tinkphp是国内非常流行的一个开源框架,国内大小公司都在用的框架.对于初学的好多同学感觉不太好上手,其实并没没有大家想的那么复杂.自动验证功能是thinkphp提高的一种数据验证方法,分为动态和静 ...

  9. Spring----. ref的用法

      ref元素是用在property中,来设置需要引用的容器管理的其它Bean.     它的用法:<ref bean|local|parent="someBean"> ...

  10. [USACO18JAN]Cow at Large G(树形DP)

    P4186 [USACO18JAN]Cow at Large G(树形DP) Luogu4186 设dp[i]表示i点需要放多少个农民.则有 \(if(near[i]-dep[i]<=dep[i ...