hdu5353Average
来源:互联网 发布:mysql 修改列字符集 编辑:程序博客网 时间:2024/06/05 20:06
枚举第一个人对第二个人的三种操作,
然后这样第i个人必然对第i+1个人按照1.少一个糖果就从下一个人手中拿一个
2.多一个就给一个给下一个人
3.刚好就是平均数跳过
4.无解
这四种操作贪心即可。
#include<map>#include<string>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<iostream>#include<algorithm>#include<bitset>#include<climits>#include<list>#include<iomanip>#include<stack>#include<set>using namespace std;typedef long long ll;int a[100010],b[100010];vector<int>ans;bool isok(int n,int av){for(int i=0;i<n;i++)b[i]=a[i];for(int i=1;i<n;i++){int t=b[i]-av,j=(i+1)%n;if(t==-1){b[i]++;b[j]--;ans.push_back(j);ans.push_back(i);}else if(t==0)continue;else if(t==1){b[i]--;b[j]++;ans.push_back(i);ans.push_back(j);}elsereturn 0;}for(int i=0;i<n;i++)if(b[i]!=av)return 0;return 1;}void print(){puts("YES");cout<<ans.size()/2<<endl;for(int i=0;i<ans.size();i+=2)cout<<ans[i]+1<<" "<<ans[i+1]+1<<endl;}int main(){int T;scanf("%d",&T);while(T--){int n;scanf("%d",&n);ll sum=0;for(int i=0;i<n;i++){scanf("%d",a+i);sum+=a[i];}if(sum%n!=0){puts("NO");continue;}sum/=n;int av=int(sum);ans.clear();if(isok(n,av))print();else{a[0]--;a[1]++;ans.clear();ans.push_back(0);ans.push_back(1);if(isok(n,av))print();else{a[0]+=2;a[1]-=2;ans.clear();ans.push_back(1);ans.push_back(0);if(isok(n,av))print();elseputs("NO");;}}}}
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 127 Accepted Submission(s): 16
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
Source
2015 Multi-University Training Contest 6
1 0
- hdu5353Average
- 制作私有pod库的过程总结
- 一行代码巧妙实现iOS返回按钮
- Codeforces A. Lineland Mail
- 二分查找
- 双线程DP (三维法四维法) sdnu 1032.机器人 1194.传纸条
- hdu5353Average
- 【读书笔记】iOS-验证应用内支付的凭证注意事项
- Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags()
- ERROR: Can't get master address from ZooKeeper; znode data == null
- Xcode6 新建 Prefix Header
- storyboard 和 xib混用出现的layout布局有问题
- scala学习之map和tuple
- JPA入门例子(采用JPA的hibernate实现版本)
- 1030 - Discovering Gold (lightoj 1030 概率DP)