模拟,,,

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

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

#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. PHP判断是否微新浏览器

    $inwechat = false; $user_agent = $_SERVER['HTTP_USER_AGENT']; if (strpos($user_agent, 'MicroMessenge ...

  2. The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. (关于jdbc)

    The last packet sent successfully to the server was milliseconds ago. The driver has not received an ...

  3. spark1.1.0源码阅读-executor

    1. executor上执行launchTask def launchTask( context: ExecutorBackend, taskId: Long, taskName: String, s ...

  4. 设定PCB电路板形状和物理边界

    1 设定PCB电路板形状和物理边界 在Protel DXP的PCB板文件向导中,我们已经初步确定了电路板的形状和物理边界.但我们在绘制PCB板之前,也许还会对电路板的边界的细节加以调整.如果我们要对电 ...

  5. ZOJ 2760 How Many Shortest Path

    题目大意:给定一个带权有向图G=(V, E)和源点s.汇点t,问s-t边不相交最短路最多有几条.(1 <= N <= 100) 题解:从源点汇点各跑一次Dij,然后对于每一条边(u,v)如 ...

  6. NCPC 2015 - Problem A - Adjoin the Networks

    题目链接 : http://codeforces.com/gym/100781/attachments 题意 : 有n个编号为0-n-1的点, 给出目前已经有的边(最多n-1条), 问如何添加最少的边 ...

  7. iperf网络测试工具

    iperf https://sourceforge.net/projects/iperf/ http://downloads.es.net/pub/iperf/ https://github.com/ ...

  8. 将vs2012建的项目转换为vs2010项目

    vs2012建的项目vs2010打不开,但vs2010的项目vs2012能打开,所以我觉得vs2012没有对解决方案进行重大的调整,尝试修改了一下.sln文件,使用vs2010就能打开了,方法如下: ...

  9. Xcode Build Setting Reference

    https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/ ...

  10. 共享IP云主机(VPS)玩转wdcp

    目前国内有不少性能还不错的共享IP VPS,但因为没有独立IP,所以环境配置起来会比较麻烦. 因为本人自己现在用的就是共享IP的vps,所以把一些配置方法分享一下,供大家参考. 首先是系统的选择,根据 ...