hdu5353
来源:互联网 发布:plc,单片机,dsp的区别 编辑:程序博客网 时间:2024/05/21 08:45
Average
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2992 Accepted Submission(s): 720
Special Judge
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 sodax 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.
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
1.
2.
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 integern (1≤n≤105) , the number of soda.
The next line containsn integers a1,a2,…,an (0≤ai≤109) , where ai denotes the candy i -th soda has.
The first contains an integer
The next line contains
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
361 0 1 0 0 051 1 1 1 131 2 3
Sample Output
NOYES0YES22 13 2
Author
zimpha@zju
Source
2015 Multi-University Training Contest 6
Recommend
wange2014
题意:n个人,围城一个圈,每个人有一些糖果,每个人只能给他左边或右边那个人
一个糖果一次,问能否使所有人最后糖果数量相同
每个人只能传递一次,那么如果某个人有的糖果比平均值>2或<2肯定是无解的;
我们的做法是将每个人的糖果数减去平均值,那么这个数列就变成0 1 -1 2 -2 的序列了,包含3 -3 就是无解,当然所有数和要能够整除n是前提;
然后用一个二维数组保存状态 g[i][0] g[i][1],g[1][0]表示第i个向左边给一个糖果
g[i][1]表示向右边给一个糖果,然后用一个0-2*n的循环表示n个人围成的圈
依次判断当前与右边项的传递关系,最后统计传递次数,输出g[][]数组
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;typedef long long LL;const int maxn=1e5+10;int g[maxn][2];int a[maxn];int main(){ int t,n; int x,y; scanf("%d",&t); while(t--) { scanf("%d",&n); LL sum=0; memset(g,0,sizeof(g)); for(int i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } //cout<<"ff"<<endl; if(sum%n) { puts("NO"); continue; } sum/=n; int flag=1; for(int i=0;i<n;i++) { a[i]-=sum; if(a[i]>2) flag=0; } // cout<<"dd"<<endl; for(int i=0;i<2*n;i++) { x=i%n; y=(i+1)%n; if(a[x]>0&&a[y]<=0&&!g[x][1])///x没有向右给予 { a[x]--; a[y]++; if(!g[y][0])///y也没有向左给予 g[x][1]=1; else g[x][1]=g[y][0]=0;///y向左给予,两者抵消 } else if(a[x]<0&&a[y]>=0&&!g[y][0])///类似上面 { a[x]++; a[y]--; if(!g[x][1]) g[y][0]=1; else g[x][1]=g[y][0]=0; } } int temp=0; for(int i=0;i<n;i++) { if(a[i])///最终结果a全部变成0 { flag=0; break; } temp+=g[i][0]+g[i][1]; } if(flag) { printf("YES\n%d\n",temp); for(int i=1;i<=n;i++) { x=i-1; y=i+1; if(x==0) x=n; if(y==n+1) y=1; if(g[i-1][0]) printf("%d %d\n",i,x); if(g[i-1][1]) printf("%d %d\n",i,y); } } else { puts("NO"); } } return 0;}
0 0
- hdu5353
- hdu5353
- hdu5353
- hdu5353 模拟
- HDU5353 Average
- hdu5353 Average
- hdu5353 (贪心)
- HDU5353:Average(贪心)
- 枚举 - hdu5353 Average
- hdu5353(2015多校6)--Average(贪心)
- HDU5353.Average(多校第六场)
- 2015 多校联赛 ——HDU5353(构造)
- [枚举+贪心] hdu5353多校联合 第六场 Average
- hdu5353||2015多校联合第六场1001 贪心
- 2015 Multi-University Training Contest 6(hdu5353-5363)
- SpringMVC与Struts2区别与比较总结 .
- 【Java】编程细节与技巧
- 树路径和总结
- (第1讲)动态网页的发展(韩顺平的视频教程+自己看的书)
- MATLAB面向对象编程
- hdu5353
- nyoj36 DP动规 最长公共子序列
- 《大话Java性能优化》面向对象及基础类型相关部分
- RTMP send error 104(129 bytes)解决方法
- jquery 将disabled的元素置为enabled的三种方法
- Linux(CentOS 7.0)安装Oracle11g R2 64位
- shader学习之路(3)-纹理压缩与混合
- Android自定义组合控件实现动态轮播图
- Firebase初探:实时数据库(1)