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 ...
随机推荐
- spring Batch实现数据库大数据量读写
spring Batch实现数据库大数据量读写 博客分类: spring springBatchquartz定时调度批处理 1. data-source-context.xml <?xml v ...
- 用jquery ajax做的select菜单,选中的效果
//用server端语言赋值给js变量 var departmentId = '<%=提交的值 %>', deviceId='<%=提交的值 %>' $(fun ...
- c7---函数
// // main.c // 函数练习 // // Created by xiaomage on 15/6/7. // Copyright (c) 2015年 xiaomage. All right ...
- BZOJ1492:[NOI2007]货币兑换 (CDQ分治+斜率优化DP | splay动态维护凸包)
BZOJ1492:[NOI2007]货币兑换 题目传送门 [问题描述] 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和B纪念券(以下简称B券).每个持有金券的 ...
- File and Folder Permissions
https://msdn.microsoft.com/en-us/library/bb727008.aspx On NTFS volumes, you can set security permiss ...
- 终端安全工具 gartner 排名
Reviews for Endpoint Detection and Response Solutions What is Endpoint Detection and Response Soluti ...
- 客户现场调试(连接oracle数据库)
1.System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本 http://blog.csdn.net/yucaoye/article/details/ ...
- POJ 1543 暴搜
题意:输出a^3=b^3+c^3+d^3的所有a,b,c,d的值. b,c,d由小到大且b,c,d都大于1. 思路: 按照题意写就好.... // by SiriusRen #include < ...
- SqlServer Function 实例
① sql server function 创建 这里使用一个计算年龄精确到分的function作为一个demo, create Function [dbo].[fn_GetAge] ( @BIRTH ...
- Android Span的简单使用
Spanable中的常用常量: Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含start和end所在的端点 (a,b) Span ...