HDU 5353 Average
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.
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.
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.
3
6
1 0 1 0 0 0
5
1 1 1 1 1
3
1 2 3
NO这题要注意可能会有从n绕回1的,或者从1绕到n的。感觉情况比較多。 。必须注意2的情况。。
YES
0
YES
2
2 1
3 2#include<cstdio>
#include<cmath>
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn=100005;
ll T,n,m,a[maxn],sum,tot;
bool flag;
int f[maxn][2]; int main()
{
scanf("%lld",&T);
while (T--)
{
scanf("%lld",&n);
sum=0; flag=true;
for (int i=0;i<n;i++)
{
scanf("%lld",&a[i]);
sum+=a[i];
}
if (sum%n) flag=false;
else
{
sum=sum/n;
tot=0;
for (int i=0;i<n;i++) a[i]-=sum;
memset(f,0,sizeof(f));
for (int i=0,j,k;i<n+n;i++)
{
j=i%n; k=(i+1)%n;
if (a[j]>0&&a[k]<=0&&!f[j][1]) {
a[k]++;
a[j]--;
if (!f[k][0]) f[j][1]=1;
else f[k][0]=f[j][1]=0;
}
else if (a[j]<0&&a[k]>=0&&!f[k][0]) {
a[k]--;
a[j]++;
if (!f[j][1]) f[k][0]=1;
else f[j][1]=f[k][0]=0;
}
}
for (int i=0;i<n;i++) if (a[i]) {flag=false; break;}
for (int i=0;i<n;i++) tot+=f[i][0]+f[i][1];
if (n==2&&tot==2) flag=false;
}
if (flag)
{
printf("YES\n%d\n",tot);
for (int i=1,j,k;i<=n;i++)
{
j=i-1; if (j==0) j=n;
k=i+1; if (k>n) k=1;
if (f[i-1][0]) printf("%d %d\n",i,j);
if (f[i-1][1]) printf("%d %d\n",i,k);
}
}
else printf("NO\n");
}
return 0;
}
HDU 5353 Average的更多相关文章
- 2015多校第6场 HDU 5353 Average 贪心,细节处理
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5353 题意:有n个人围城一个环,每一个人手里都有一些糖果,第i个人有ai块.现在有三种操作:第i个人给 ...
 - HDU 5353—— Average——————【贪心+枚举】
		
Average Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
 - HDU 5353 Average 糖果分配(模拟,图)
		
题意:有n个人坐在圆桌上,每个人带着糖果若干,每次只能给旁边的人1科糖果,而且坐相邻的两个人最多只能给一次(要么你给我,要么我给你),问是否能将糖果平均分了. 思路: 明显每个人最多只能多于平均值2个 ...
 - HDU 5353 Average 贪心
		
就是贪心啊,不知道为啥总是不过,总是WA 方法不对吗? 将数组扩展一倍,从左到右扫描,大于平均数就给右边的,小于就从右边拿,等于就不变,记录下操作类型. 大于2直接NO,不知道哪错了,自己出了一些数据 ...
 - 思维/构造 HDOJ 5353 Average
		
题目传送门 /* 思维/构造:赛后补的,当时觉得3题可以交差了,没想到这题也是可以做的.一看到这题就想到了UVA_11300(求最小交换数) 这题是简化版,只要判断行不行和行的方案就可以了,做法是枚举 ...
 - hdu 2736 Average distance
		
传送门 Average distance Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
 - HDU 5353
		
题目大意: 相邻的朋友可以给出自己手上最多一颗糖,n个朋友形成一个环,问给的方式能否最后使所有朋友都糖的数量相同 这里我用的是网络流来做的,这里n=100000,用sap的模板可以跑过 #includ ...
 - hdu 1036 (I/O routines, fgets, sscanf, %02d, rounding, atoi, strtol)                                                       分类:            hdoj             2015-06-16 19:37    32人阅读    评论(0)    收藏
		
thanks to http://stackoverflow.com/questions/2144459/using-scanf-to-accept-user-input and http://sta ...
 - Average(模拟)
		
Average Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tota ...
 
随机推荐
- CSDN--十年
			
昨天获得了博客专家的勋章,惊喜总是来的有点意外.这个勋章也算是来的一波三折.借着这个机会,回首一下我在CSDN的博客历史. 这个博客如今可查的最早的文章,是04年下半年写的,事实上之前应该另一些自己写 ...
 - C++类中静态变量和静态方法的注意事项
			
在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量仅仅存储一份供全部对象共用.所以在全部对象中都能够共享它.使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则,保证了安全性还能够 ...
 - Chisel Tutorial(一)——Chisel介绍
			
Chisel是由伯克利大学公布的一种开源硬件构建语言,建立在Scala语言之上,是Scala特定领域语言的一个应用,具有高度參数化的生成器(highly parameterized generator ...
 - 【DataStructure】One of queue usage: Simulation System
			
Statements: This blog was written by me, but most of content is quoted from book[Data Structure wit ...
 - Oracle在Linux下的性能优化
			
Oracle数据库内存参数的优化 Ø 与oracle相关的系统内核参数 Ø SGA.PGA参数设置 Oracle下磁盘存储性能优化 Ø 文件系统的选择(ext2 ...
 - C#操作sql时注意点
			
①创建必要的索引 ②使用预编译查询 ③使用参数化sql会执行预编译,第一次执行的时候DBMS会为这个SQL语句进行查询优化并执行预编译 ④调整where子句中的连接顺序 ⑤DBMS一般次用自上而下的顺 ...
 - SpringBoot(八)   Spring和消息队列RabbitMQ
			
概述 1.大多数应用中,可以通过消息服务中间件来提升系统异步能力和拓展解耦能力. 2.消息服务中的两个重要概念:消息代理(Message broker)和目的地(destination) 当消息发送者 ...
 - 自定义view 之多个引导层动画效果
			
SupernatantView 如果我英文还可以的话这个应该叫做漂浮在上层的view---引导层 今天闲来无事看了网上的一些引导层案例总感觉如果不是很舒服,就是类似于很死板的显示和消失 我在想能不能弄 ...
 - 快速搭建golden gate环境
			
准备操作系统用户和文件系统 准备安装和运行用户(操作系统用户) 建议使用Oracle用户 也可使用新建用户,但需做以下配置 该用户必须加入到oinstall组 该用户使用与oracle相同的profi ...
 - hdu 5375 Gray code 【 dp 】
			
dp[i][j]表示第i位取j的时候取得的最大的分数 然后分s[i]是不是问号,s[i-1]是不是问号这大的四种情况讨论 #include<cstdio> #include<cstr ...