hdu5353
模拟,,,
每个人有一些糖果,每两个人之间只能给一个糖果,问最后是否能让所有人的糖果数量相同,只要确定一个糖果的流向其他的就能够确定。
马虎了,卡了好几天,心塞塞的。。。
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std; const int maxa = ;
int biaoji[maxa];
int vis[maxa];
int put[maxa][];
int ans(int a[], int n){
int sta;
int o = ;
for(int i =;i < n; i++){
if(a[i] > ){
sta = i;
break;
}
}
memset(vis, , sizeof(vis));
int len = ;
int last = ;
for(int k = sta;; k++){
if(len++ > n*) break;
int i = k % n;
if(vis[i] == ){
last = ;
continue;
}
if(last == && a[i] == ){
last = ;
continue;
}
if(a[i] > ){
last = ;
put[o][] = i;
put[o++][] = (i+)%n;
vis[i] = ;
a[i] --, a[(i+)%n] ++;
}else if(a[i] < ) last = ;
}
/*for(int i =0 ;i < n; i++){
printf("%d ", a[i]);
}puts("");*/
len = ;
last = ;
for(int i = sta; ; i--){
i = (i%n+n*)%n;
if(len++ > n*) break;
if(vis[(i+n-)%n] == )continue;
if(a[i] > ){
put[o][] = i;
put[o++][] = (i+n-)%n;
vis[(i+n-)%n] = ;
a[i] --;
a[(i-+n)%n] ++;
}
}
for(int i = ;i < n; i++){
if(a[i] != ) return ;
}
puts("YES");
printf("%d\n", o);
for(int i = ;i < o; i++){
printf("%d %d\n", put[i][]+, put[i][]+);
}return ;
}
int ans1(int a[], int n){
int sta;
int o = ;
for(int i =;i < n; i++){
if(a[i] > ){
sta = i;
break;
}
}
memset(vis, , sizeof(vis));
int len = ;
int last = ; for(int i = sta; ; i--){
i = (i%n+n)%n;
if(last == && a[i] == ){
last = ;
continue;
} if(len++ > n*) break;
if(vis[(i+n-)%n] == ){
last = ;
continue;
}
if(a[i] > ){
put[o][] = i;
put[o++][] = (i+n-)%n;
vis[(i+n-)%n] = ;
a[i] --;
a[(i-+n)%n] ++;
}else if(a[i] < ){
last = ;
}
}
/*for(int i =0 ;i < n; i++){
printf("%d ", a[i]);
}puts("");*/
len = ;
last = ;
for(int k = sta;; k++){
if(len++ > n*) break;
int i = k % n;
if(vis[i] == )continue;;
if(a[i] > ){
last = ;
put[o][] = i;
put[o++][] = (i+)%n;
vis[i] = ;
a[i] --, a[(i+)%n] ++;
}
}
for(int i = ;i < n; i++){
if(a[i] != ) return ;
}
puts("YES");
printf("%d\n", o);
for(int i = ;i < o; i++){
printf("%d %d\n", put[i][]+, put[i][]+);
}return ;
}
int a[maxa], b[maxa], c[maxa];
int main(){
int t;
scanf("%d", &t);
while(t--){
memset(biaoji, , sizeof(biaoji));
int n;
scanf("%d", &n);
long long sum = ;
int ok = ;
for(int i = ;i < n; i++){
scanf("%d", &a[i]);
sum += a[i];
}
for(int i = ; i < n; i++){
if(a[i] != a[i-]){
ok = ;
break;
}
}
int ook = ;
for(int i = ;i < n; i++){
b[i] = a[i] - sum/n;
c[i] = b[i];
if(c[i] > )biaoji[i] = ;
else biaoji[i] = ;
if(b[i] > || b[i] < -) ook =;
}
if(ok == ){
puts("YES\n0");
continue;
}
if(sum % n != || ook){
puts("NO");
continue;
}
if(ans(b, n));
else if(ans1(c, n));
else puts("NO");
}
}
/*
5
6
2 0 0 2 0 2
8
3 0 0 3 0 2 0 0
8
3 0 0 3 0 0 2 0
8
0 0 3 3 0 0 2 0
8
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000001
*/
这是a过后花了二十分钟敲的不那么丑但依旧很丑的代码
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxa = ;
int put[maxa][], vis[maxa];
int ans(int n, int a[]){
int sta= ;
for(int i = ;i < n; i++){
if(a[i] > ){
sta = i;
}
}
memset(vis, , sizeof(vis));
int len = , last = , o = ;
for(int i = sta; ;i++){
i = i%n;
if(len++ > *n) break;
if(vis[i] == ){
last= ;
continue;
}
if(last == && a[i] == ){
last = ;
continue;
}
if(a[i] > ){
vis[i] = ;
a[i] --, a[(i+)%n] ++;
put[o][] = i;
put[o][] = (i+)%n;
o++;
}else if(a[i] < ){
last = ;
}
}
len = , last = ;
for(int i = sta; ; i--){
i = (i+n)%n;
if(len++ > n*) break;
if(vis[(i+n-)%n] == ){
last = ;
continue;;
}
if(last == && a[i] == ){
last = ;
continue;
}
if(a[i] > ){
vis[(i+n-)%n] = ;
a[i] --, a[(i+n-)%n] ++;
put[o][] = i;
put[o][] = (i+n-)%n;
o++;
}else if(a[i] < ) last = ;
}
for(int i = ; i< n; i++){
if(a[i]) return ;
}
puts("YES");
printf("%d\n", o);
for(int i = ;i < o; i++){
printf("%d %d\n", +put[i][], +put[i][]);
}
return ;
}
int ans1(int n, int a[]){
int sta= ;
for(int i = ;i < n; i++){
if(a[i] > ){
sta = i;
}
}
memset(vis, , sizeof(vis));
int len = , last = , o = ;
for(int i = sta; ; i--){
i = (i+n)%n;
if(len++ > n*) break;
if(vis[(i+n-)%n] == ){
last = ;
continue;;
}
if(last == && a[i] == ){
last = ;
continue;
}
if(a[i] > ){
vis[(i+n-)%n] = ;
a[i] --, a[(i+n-)%n] ++;
put[o][] = i;
put[o][] = (i+n-)%n;
o++;
}else if(a[i] < ) last = ;
}
len = , last = ;
for(int i = sta; ;i++){
i = i%n;
if(len++ > *n) break;
if(vis[i] == ){
last= ;;continue;
}
if(last == && a[i] == ){
last = ;
continue;
}
if(a[i] > ){
vis[i] = ;
a[i] --, a[(i+)%n] ++;
put[o][] = i;
put[o][] = (i+)%n;
o++;
}else if(a[i] < ){
last = ;
}
}
for(int i = ; i< n; i++){
if(a[i]) return ;
}
puts("YES");
printf("%d\n", o);
for(int i = ;i < o; i++){
printf("%d %d\n", +put[i][], +put[i][]);
}
return ;
}
int a[maxa], b[maxa], c[maxa];
int main(){
int n, t;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
long long sum = ;
for(int i = ;i < n; i++){
scanf("%d", &a[i]);
sum += a[i];
}
if(sum % n){
puts("NO"); continue;
}
for(int i = ;i < n; i++){
b[i] = a[i] - sum /n;
c[i] = b[i];
}
if(ans(n, b)) ;
else if(ans1(n, c));
else puts("NO");
}
}
hdu5353的更多相关文章
- hdu5353 Average(模拟)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Average Time Limit: 4000/2000 MS (Java/Ot ...
- hdu5353 Average
Problem Description There are n soda sitting around a round table. soda are numbered from 1 to n and ...
- 2015 多校联赛 ——HDU5353(构造)
Each soda has some candies in their hand. And they want to make the number of candies the same by do ...
- [hdu5353]模拟
题意:有n个小朋友,每个小朋友手上有一些糖,考虑每两个相邻的小朋友a.b,可以选择执行3种操作中的任一种:(1)a给b一粒糖(2)b给a一粒糖(3)不进行任何动作,问能否通过确定每两个相邻的小朋友的操 ...
随机推荐
- Thinkphp 数据的修改及删除操作
一.数据修改操作 save() 实现数据修改,返回受影响的记录条数 具体有两种方式实现数据修改,与添加类似(数组.AR方式) 1.数组方式: a) $goods = D(“Goods ...
- 禁止ios默认拉动弹性行为
document.ontouchmove = function(e) { e.preventDefault();} /** 禁止ios默认拉动弹性行为**///屏蔽ios下浏览器滚动缓冲效果
- sudo gedit xx warning
When I sue command "sudo gedit xx", it appeas several warning: gedit:): IBUS-WARNING **: T ...
- C# 文件创建时间,修改时间
System.IO.FileInfo fi = new System.IO.FileInfo(@"D:\site\EKECMS\skin\Grey\default#.html"); ...
- 关于Layouts的分类
ViewGroup ---------------------------------LinearLayout.Relative Layout. AdapterView --------------- ...
- 基本排序算法:Python实现
基本排序算法,包括冒泡排序,插入排序,选择排序,堆排序,快速排序等. [冒泡排序] 复杂度是n*n #coding:utf8 #author:HaxtraZ #description:冒泡排序 def ...
- HDU_1016——素环问题DFS
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- HDU - 5156 Harry and Christmas tree
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=5156 题意 : 给一颗编号为1-n的以1为根的树, 已知有m个颜色的礼物分布在某些节点上(同一节点 ...
- openstack 手动安装版 功能测试
nova network-create demo-net --bridge br100 --multi-host T --gateway 192.168.3.252 --dns1 202.102.19 ...
- Hadoop在Windows环境下的部署[转]
http://blog.csdn.net/ruby97/article/details/7423088 经过一整天的折腾,参考了网上很多资料,我机器上的Hadoop似乎是配置成功了.下面分享一下详细的 ...