博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5353 Average
阅读量:4326 次
发布时间:2019-06-06

本文共 2220 字,大约阅读时间需要 7 分钟。

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 
(1n105), the number of soda.
The next line contains 
n integers 
a1,a2,,an 
(0ai109), 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 
(0mn) in the second line denoting the number of operations needed. Then each of the following 
m lines contain two integers 
x and 
y 
(1x,yn), 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
#include
#include
#include
#include
#include
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
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) 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;}

转载于:https://www.cnblogs.com/jhcelue/p/6937510.html

你可能感兴趣的文章
2016 - 1 -17 GCD学习总结
查看>>
linux安装php-redis扩展(转)
查看>>
Vue集成微信开发趟坑:公众号以及JSSDK相关
查看>>
技术分析淘宝的超卖宝贝
查看>>
i++和++1
查看>>
react.js
查看>>
P1313 计算系数
查看>>
NSString的长度比较方法(一)
查看>>
Azure云服务托管恶意软件
查看>>
My安卓知识6--关于把项目从androidstudio工程转成eclipse工程并导成jar包
查看>>
旧的起点(开园说明)
查看>>
生产订单“生产线别”带入生产入库单
查看>>
crontab导致磁盘空间满问题的解决
查看>>
java基础 第十一章(多态、抽象类、接口、包装类、String)
查看>>
Hadoop 服务器配置的副本数量 管不了客户端
查看>>
欧建新之死
查看>>
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
mysql 日期时间运算函数(转)
查看>>