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个人均分蛋糕。刚開始每人都有a[i]块蛋糕。每一个人有一次机会能把自己的一个苹果传给左边的人或者右边的人(注意是环状的)。问能不能使最后每一个人的蛋糕数都同样。 思路:先看总和能不能被n均分,不能均分就输出NO,假设能均分。算出平均值ave,把每一个a[i]变为a[i]-ave,假设a[i]的平均值大于等于3就输出NO,假设是2或者-2,就把它拆成两个1或者两个-1. 上面处理完后。就先找到第一个1的位置(假设找不到。说明都是0,直接输出YES),然后对于这个1。他仅仅能分给右边的-1或者左边的-1,并且在遇到-1前不能再遇到1(能够画一下),当它走到原位置的时候。代表可行,假设中途就不可行就输出NO。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 100050
#define ll long long
struct node{
int idx,num;
}b[2*maxn];
struct edge{
int l,r;
}c[2*maxn]; int vis[2*maxn],tot,m;
ll a[2*maxn];
void daying(int st,int ed,int f)
{
int i,j,k;
if(f==2){
if(st<ed){
for(k=st;k<ed;k++){
tot++;c[tot].l=b[k].idx;c[tot].r=b[k+1].idx;
}
}
else{
for(k=st;k<=m-1;k++){
tot++;c[tot].l=b[k].idx;c[tot].r=b[k+1].idx;
}
tot++;c[tot].l=b[m].idx;c[tot].r=b[1].idx;
for(k=1;k<ed;k++){
tot++;c[tot].l=b[k].idx;c[tot].r=b[k+1].idx;
}
}
}
else if(f==1){
if(st>ed){
for(k=st;k>ed;k--){
tot++;c[tot].l=b[k].idx;c[tot].r=b[k-1].idx;
}
}
else{
for(k=st;k>=2;k--){
tot++;c[tot].l=b[k].idx;c[tot].r=b[k-1].idx;
}
tot++;c[tot].l=b[1].idx;c[tot].r=b[m].idx;
for(k=m;k>ed;k--){
tot++;c[tot].l=b[k].idx;c[tot].r=b[k-1].idx;
}
}
}
} int main()
{
int i,j,T,flag,num,st,p,kaishi;
ll sum,ave,n;
scanf("%d",&T);
while(T--)
{
sum=0;
scanf("%lld",&n);
for(i=1;i<=n;i++){
scanf("%lld",&a[i]);
sum+=a[i];
}
if(sum%n!=0){
printf("NO\n");continue;
}
ave=sum/n;flag=1;
m=0;
for(i=1;i<=n;i++){
a[i]=a[i]-ave;
if(abs(a[i])>=3){
flag=0;break;
}
if(a[i]==1 || a[i]==0 || a[i]==-1){
m++;b[m].idx=i;b[m].num=a[i];
}
else{
m++;b[m].idx=i;b[m].num=a[i]/2;
m++;b[m].idx=i;b[m].num=a[i]/2;
}
}
if(flag==0){
printf("NO\n");continue;
}
kaishi=0;
for(i=1;i<=m;i++){
if(b[i].num==1){
kaishi=i;break;
}
}
if(kaishi==0){
printf("YES\n");
printf("0\n");continue;
}
tot=0;
memset(vis,0,sizeof(vis));
num=1;p=st=kaishi;vis[kaishi]=1;
flag=1;
while(1) //往右方向找
{
if(p==m){
p=1;
}
else p++;
if(vis[p]==1)break;
vis[p]=1;
if(b[p].num==0){
continue;
}
if(num==0){
st=p;
num+=b[p].num;
continue;
} num+=b[p].num;
if(abs(num)>=2){
flag=0;break;
}
if(b[p].num==1){
daying(p,st,1);
}
else{
daying(st,p,2);
}
//st=p;
}
if(flag){
printf("YES\n");
printf("%d\n",tot);
for(i=1;i<=tot;i++){
printf("%d %d\n",c[i].l,c[i].r);
}
continue;
} tot=0;
memset(vis,0,sizeof(vis));
num=1;p=st=kaishi;vis[kaishi]=1;
flag=1;
while(1) //忘左方向找
{
if(p==1){
p=m;
}
else p--;
if(vis[p]==1)break;
vis[p]=1;
if(b[p].num==0){
continue;
}
if(num==0){
st=p;
num+=b[p].num;
continue;
} num+=b[p].num;
if(abs(num)>=2){
flag=0;break;
}
if(b[p].num==1){
daying(p,st,2);
}
else{
daying(st,p,1);
}
}
if(flag){
printf("YES\n");
printf("%d\n",tot);
for(i=1;i<=tot;i++){
printf("%d %d\n",c[i].l,c[i].r);
}
continue;
}
printf("NO\n");
}
return 0;
}
/*
100
11
1 -1 1 1 -1 0 1 -1 -1 1 -1
*/

hdu5353 Average的更多相关文章

  1. hdu5353 Average(模拟)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Average Time Limit: 4000/2000 MS (Java/Ot ...

  2. training 2

    Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.136 Average Precision (AP) @[ IoU ...

  3. [LeetCode] Moving Average from Data Stream 从数据流中移动平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  4. Average Precision of VOC

    转一篇文章,主要是关于VOC中Average Precision指标的 原文出处:https://sanchom.wordpress.com/tag/average-precision/ 还有一篇文章 ...

  5. Moving Average from Data Stream

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  6. average slice

    A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q), such ...

  7. LeetCode Moving Average from Data Stream

    原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...

  8. Linq查询操作之聚合操作(count,max,min,sum,average,aggregate,longcount)

    在Linq中有一些这样的操作,根据集合计算某一单一值,比如集合的最大值,最小值,平均值等等.Linq中包含7种操作,这7种操作被称作聚合操作. 1.Count操作,计算序列中元素的个数,或者计算满足一 ...

  9. Load Average

    在Linux系统下面,有很多的命令可以查看系统的负载情况:比如top,uptime,w,示例如下: [wenchao.ren@l-cmsweb1.ops.cn1 ~]$ w 18:39:10 up 7 ...

随机推荐

  1. 【2】hadoop搭建准备软件

    准备一:VMware虚拟工具: 链接:http://pan.baidu.com/s/1o7F4A6I 密码:w5ti 准备二:CentOS6.8虚拟机(64位):如果64位不允许安装,可能是电脑设置问 ...

  2. SharpGL(46)用Billboard绘制头顶文字

    CSharpGL(46)用Billboard绘制头顶文字 本文介绍CSharpGL用Billboard绘制头顶文字的方法.效果如下图所示. 下载 CSharpGL已在GitHub开源,欢迎对OpenG ...

  3. OGEngine_2.x中BitmapFont加载后黑屏问题的解决办法

    在我使用OGEngine_2.x进行消灭圈圈(星星)游戏的实践的时候,使用BitmapFont对自定义字体进行调用. 原文字体教程如下:http://blog.csdn.net/OrangeGame/ ...

  4. java继承系列之添加一个LinkLable类

    import java.awt.*; import javax.swing.*; import javax.swing.JFrame; import java.awt.event.WindowList ...

  5. tornado的非异步阻塞模式

    [优化tornado阻塞任务的三个选择] 1.优化阻塞的任务,使其执行时间更快.经常由于是一个DB的慢查询,或者复杂的上层模板导致的,这个时候首要的是加速这些任务,而不是优化复杂的webserver. ...

  6. Python之numpy模块array简短学习

    1.简介 Python的lists是非常的灵活以及易于使用.但是在处理科学计算相关大数量的时候,有点显得捉襟见肘了. Numpy提供一个强大的N维数组对象(ndarray),包含一些列同类型的元素,这 ...

  7. P2704 炮兵阵地

    题目描述 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用“H” 表示),也可能是平原(用“P”表示),如下图.在每一格平原地形上最 ...

  8. 宏WINAPI和几种调用约定

    在VC SDK的WinDef.h中,宏WINAPI被定义为__stdcall,这是C语言中一种调用约定,常用的还有__cdecl和__fastcall.这些调用约定会对我们的代码产生什么样的影响?让我 ...

  9. RecyclerView分割线——万能分割线

    参照网络上众多的分割线设计方法,对方法进行调整和修改,最终完成的比较通用的RecyclerView分割线,底部会附上参考网址,大家可以去看一下. 在正文之前,先说一下个人看法:研究下来,我发现,其实最 ...

  10. 六、Hadoop学习笔记————调优之操作系统以及JVM

    内核参数overcommit_memory  它是 内存分配策略 可选值:0.1.2.0, 表示内核将检查是否有足够的可用内存供应用进程使用:如果有足够的可用内存,内存申请允许:否则,内存申请失败,并 ...