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. loj #2025. 「JLOI / SHOI2016」方

    #2025. 「JLOI / SHOI2016」方   题目描述 上帝说,不要圆,要方,于是便有了这道题. 由于我们应该方,而且最好能够尽量方,所以上帝派我们来找正方形.上帝把我们派到了一个有 NNN ...

  2. 主要介绍JavaEE中Maven Web 项目的结构及其它几个小问题

    先说下本篇随笔的目录. 1.介绍windows中环境变量Path与ClassPath的区别. 2.可能导致命令行运行javac编译成功,但 java命令 + 所要执行的类的类名 无效的原因. 3.介绍 ...

  3. ORACLE 中dbms_stats的使用

    dbms_stats能良好地估计统计数据(尤其是针对较大的分区表),并能获得更好的统计结果,最终制定出速度更快的SQL执行计划. exec dbms_stats.gather_schema_stats ...

  4. 公有云Docker镜像P2P加速之路:安全篇

    一.问题 在使用Docker运行容器化应用时,宿主机通常先要从Registry服务(如Docker Hub)下载相应的镜像(image).这种镜像机制在开发环境中使用还是很有效的,团队成员之间可以很方 ...

  5. linux文件系统总结

    apue中:其中进程表项内部的数组又称为 进程打开文件表    另外一个角度: 从linux内核角度开: task_struct是进程描述符对应上面的进程表项,在task_struct描述符中有str ...

  6. manjaro linux KDE桌面网易云音乐点击托盘图标无反应

    这是一个很奇怪的问题,它的解决方法更奇怪... 找到网易云,右键,选择“编辑应用程序” 在打开的窗口中选中应用程序一栏 将“命令”的内容替换为: 1 env XDG_CURRENT_DESKTOP=D ...

  7. springcloud系列10 整合Hystrix遇到的坑:

    首先配置类: @Bean public ServletRegistrationBean getServlet(){ HystrixMetricsStreamServlet streamServlet ...

  8. Report Server运行后一直处于加载状态

    描述:对Report server做了一个小练习,算是入门,但发现运行起来后,页面一直处于加载状态,不知为何? 解决:查了一下网上的资料,解决的方法是 protected void Page_Load ...

  9. CodeForces-Zuhair and Strings(思维+枚举)

    Given a string ss of length nn and integer kk (1≤k≤n1≤k≤n). The string ss has a level xx, if xx is l ...

  10. hdu2063 最大二分匹配(匈牙利算法)

    过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...