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绕回1的,或者从1绕到n的。感觉情况比較多。 。必须注意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的更多相关文章

  1. 2015多校第6场 HDU 5353 Average 贪心,细节处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5353 题意:有n个人围城一个环,每一个人手里都有一些糖果,第i个人有ai块.现在有三种操作:第i个人给 ...

  2. HDU 5353—— Average——————【贪心+枚举】

    Average Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  3. HDU 5353 Average 糖果分配(模拟,图)

    题意:有n个人坐在圆桌上,每个人带着糖果若干,每次只能给旁边的人1科糖果,而且坐相邻的两个人最多只能给一次(要么你给我,要么我给你),问是否能将糖果平均分了. 思路: 明显每个人最多只能多于平均值2个 ...

  4. HDU 5353 Average 贪心

    就是贪心啊,不知道为啥总是不过,总是WA 方法不对吗? 将数组扩展一倍,从左到右扫描,大于平均数就给右边的,小于就从右边拿,等于就不变,记录下操作类型. 大于2直接NO,不知道哪错了,自己出了一些数据 ...

  5. 思维/构造 HDOJ 5353 Average

    题目传送门 /* 思维/构造:赛后补的,当时觉得3题可以交差了,没想到这题也是可以做的.一看到这题就想到了UVA_11300(求最小交换数) 这题是简化版,只要判断行不行和行的方案就可以了,做法是枚举 ...

  6. hdu 2736 Average distance

    传送门 Average distance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. HDU 5353

    题目大意: 相邻的朋友可以给出自己手上最多一颗糖,n个朋友形成一个环,问给的方式能否最后使所有朋友都糖的数量相同 这里我用的是网络流来做的,这里n=100000,用sap的模板可以跑过 #includ ...

  8. 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 ...

  9. Average(模拟)

      Average Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

随机推荐

  1. 为代码减负之&lt;三&gt;视图(SQL)

    在设计数据库时为了降低数据冗余.一般都会依照三范式去设计,但有时我们在查询时须要通过一字段获取跟这 个字段相关联的好几个字段.可是他们又分布在不同的表中,这时候假设依照正常途径走的话须要同一时候查询好 ...

  2. POJ 1236--Network of Schools【scc缩点构图 &amp;&amp; 求scc入度为0的个数 &amp;&amp; 求最少加几条边使图变成强联通】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13325   Accepted: 53 ...

  3. [BZOJ 3365] Distance Statistics

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3365 [算法] 点分治 [代码] #include <algorithm> ...

  4. linux下关于IPC(进程间通信)

    linux下进程间通信的主要几种方式 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许 ...

  5. BZOJ 3230 后缀数组+ST

    思路: 首先我们已经会了后缀数组求本质不同的子串个数 这道题跟那个差不多 首先我们可以知道按字典序排好的每个后缀之前包含多少本质不同的字串 就是sigma(n-sa[i]+1-ht[i]+bi[i-1 ...

  6. php开启CURL支持

    window下安装php_curl支持 1. 找到php.ini 修改extension=php_curl.dll 把前面的分号去掉2. 把 php_curl.dll libeay32.dll ssl ...

  7. 洛谷P3327 [SDOI2015]约数个数和(莫比乌斯反演)

    题目描述 设d(x)为x的约数个数,给定N.M,求 \sum^N_{i=1}\sum^M_{j=1}d(ij)∑i=1N​∑j=1M​d(ij) 输入输出格式 输入格式: 输入文件包含多组测试数据.第 ...

  8. 一个基于Vue.js+Mongodb+Node.js的博客内容管理系统

    这个项目最初其实是fork别人的项目.当初想接触下mongodb数据库,找个例子学习下,后来改着改着就面目全非了.后台和数据库重构,前端增加了登录注册功能,仅保留了博客设置页面,但是也优化了. 一.功 ...

  9. JSON是什么?为JavaScript准备的数据格式

    JSON是什么?为JavaScript准备的数据格式 还不了解JSON是什么?看了下面这篇文章,您对JSON是什么应该能够有了一个比较清晰的概念. JSON 即 JavaScript. Object ...

  10. day19-1 迭代器,三元表达式,列表推导式,字典生成式,

    目录 迭代器 可迭代对象 迭代器对象 总结 三元表达式(三目表达式) 列表推导式 字典生成式 迭代器 可迭代对象 拥有iter方法的对象就是可迭代对象 # 以下都是可迭代的对象 st = '123'. ...