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. 学习ASP.NET MVC系列 - 还有比这更简炼的吗?把复杂的事情变简单了,贡献啊!

    转自

  2. Openwrt 软件安装源

    进入http://downloads.openwrt.org/barrier_breaker/14.07/站点找到符合处理器型号的软件源.參考下图: watermark/2/text/aHR0cDov ...

  3. 【剑指Offer学习】【面试题26:复杂链表的复制】

    题目:请实现函数ComplexListNode clone(ComplexListNode head),复制一个复杂链表. 在复杂链表中,每一个结点除了有一个next 域指向下一个结点外,另一个sib ...

  4. Linux就该这么学 20181009(第十二章 SAMBA)

    参考链接https://www.linuxprobe.com Samba 跨平台的文件共享 linux-linux linux-windows /etc/samba/smb.conf 里面 []这个名 ...

  5. 前后端分离之接口登陆权限token

    随着业务的需求普通的springmvc+jsp已经不能满足我们的系统了,会逐渐把后台和前端展示分离开来,下面我们就来把普通的springmvc+jsp分为 springmvc只提供rest接口,前端用 ...

  6. Qt-信号和槽-1对1

    前言:信号和槽是Qt的核心机制,窗体和控件对象之间的沟通一般都使用信号和槽. 对于部件有哪些信号和槽,可以查看help文档. 一.使用自定义槽 1.1 新建工程 新建工程,新建Widget类(基于QW ...

  7. 在ubuntu下安装redis

    最简单在线安装的方式: #安装Redis服务器端 ~ sudo apt-get install redis-server 安装完成后,Redis服务器会自动启动,我们检查Redis服务器程序 # 检查 ...

  8. c#制作简单启动画面的方法

    本文实例讲述了c#制作简单启动画面的方法.分享给大家供大家参考.具体分析如下: 启动画面是程序启动加载组件时一个让用户稍微耐心等待的提示框.一个好的软件在有启动等待需求时必定做一个启动画面.启动画面可 ...

  9. 十款APP开发框架

    对于大部分Web开发人员,HTML.CSS和 Java是他们最熟练的开发技能.然而,开发一个原生的移动App,对他们来说却是完全陌生的领域.因为开发Android,iOS 或 Windows Phon ...

  10. nginx 日志配置不生效的问题

    log_format 有个默认的日志格式: log_format combined '$remote_addr - $remote_user [$time_local] ' ' "$requ ...