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)不进行任何动作,问能否通过确定每两个相邻的小朋友的操 ...
随机推荐
- Centos下删除文件名乱码文件
centos下通过rm命令来删除文件,但是如果要删除文件名乱码的文件,就不能直接使用rm命令了,因为压根就无法输出文件名来.不过借助find命令可以实现对其删除.在linux下对于每个文件都一个对应的 ...
- NUMBER BASE CONVERSION(进制转换)
Description Write a program to convert numbers in one base to numbers in a second base. There are 62 ...
- AE模板替换-众多图片沿着球体旋转!与您分享
AE-模板替换<众多图片沿球体旋转>效果的视频 近日,本人又一次做了AE的[图片替换]的模板尝试,本人感觉:预览视频时发现的效果较新颖,也有些气势;就是短了些,我安排了,&q ...
- mobile 测试入门思维导图
手机测试思维导图 ISO 测试思维导图 Android测试思维导图 原图出自:http://www.ministryoftesting.com/resources/mindmaps/
- PYTHON 正则表达示入门
确实是实践出真知,自己手打代码之后,以前停在理论上的东东,慢慢可以进入实战了. 比如,MATCH和SEARCH之间的区别. #encoding: UTF-8 import re pattern = r ...
- sigaction函数的使用
sigaction函数的功能是检查或修改与指定信号相关联的处理动作(可同时两种操作). 他是POSIX的信号接口,而signal()是标准C的信号接口(如果程序必须在非POSIX系统上运行,那么就应该 ...
- STL_vector
1.任何改变vector长度的操作都会使已经存在的迭代器失效 vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容 ...
- leetcode-Rising Temperature
Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...
- 算法导论(第三版)Exercises2.3(归并排序、二分查找、计算集合中是否有和为X的2个元素)
2.3-1: 3 9 26 38 41 49 52 59 3 26 41 52 9 38 49 57 3 41 52 26 38 57 9 49 3 41 52 26 38 ...
- 关于我们 HerlMax(赫马克斯),奢侈品顶级服装高级定制品牌!
关于我们 HerlMax(赫马克斯),奢侈品顶级服装高级定制品牌! HerlMax品牌拥有着纯正悠远的意大利高级定制文化,带着对神秘东方的向往,荣耀登入中国市场.