模拟,,,

每个人有一些糖果,每两个人之间只能给一个糖果,问最后是否能让所有人的糖果数量相同,只要确定一个糖果的流向其他的就能够确定。

马虎了,卡了好几天,心塞塞的。。。

#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的更多相关文章

  1. hdu5353 Average(模拟)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Average Time Limit: 4000/2000 MS (Java/Ot ...

  2. hdu5353 Average

    Problem Description There are n soda sitting around a round table. soda are numbered from 1 to n and ...

  3. 2015 多校联赛 ——HDU5353(构造)

    Each soda has some candies in their hand. And they want to make the number of candies the same by do ...

  4. [hdu5353]模拟

    题意:有n个小朋友,每个小朋友手上有一些糖,考虑每两个相邻的小朋友a.b,可以选择执行3种操作中的任一种:(1)a给b一粒糖(2)b给a一粒糖(3)不进行任何动作,问能否通过确定每两个相邻的小朋友的操 ...

随机推荐

  1. mysql时间int日期转换

    select from_unixtime(1350437720);select unix_timestamp(now());插入用 unix_timestamp(date)查询用from_unixti ...

  2. Python中的正斜杠与反斜杠

    首先,"/"左倾斜是正斜杠,"\"右倾斜是反斜杠,可以记为:除号是正斜杠一般来说对于目录分隔符,Unix和Web用正斜杠/,Windows用反斜杠,但是现在Wi ...

  3. 向null地址copy数据和不断改变指针指向

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...

  4. Android 自定义控件 EditText输入框两边加减按钮Button

    自己封装的一个控件:EditText两边放加减按钮Button来控制输入框的数值 Demo 下载地址: 第一版:http://download.csdn.net/detail/zjjne/674086 ...

  5. LeetCode_ Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. / ...

  6. AC自动机修正

    #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #inc ...

  7. BZOJ3301: [USACO2011 Feb] Cow Line

    3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 67  Solved: 39[Submit ...

  8. 自写 jQuery 大幅弹窗广告插件(不喜勿拍)

    最近写了做的两个项目都要做几乎同一件事,在首页弹出一个广告.本来是想在网上找一个的,找了几个,花了时间但都不怎么满意,尼玛呀,坑爹呀…… 最后一想,干脆自己动手了. 第一次写,在网上找一些例子来看. ...

  9. bzoj4096 [Usaco2013 dec]Milk Scheduling

    Description Farmer John has N cows that need to be milked (1 <= N <= 10,000), each of which ta ...

  10. cf509B Painting Pebbles

    B. Painting Pebbles time limit per test 1 second memory limit per test 256 megabytes input standard ...