题意:相邻两个人能传递一次糖果,问最后所有人的糖果数是不是一样。
把第一个给第二个的情况枚举一下,接下来剩下的人按情况即可。
按理说,依次判断 if (OK) { PRINTF; continue;}的写法和放在for里面根据case讨论第一个给第二个的情况是没有区别的,蓝而,,神奇的是前者会迷のwa-- --,顺便贴下AC代码
AC:
#include <bits/stdc++.h>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <time.h>
#include <vector>
#include <cstdio>
#include <string>
#include <iomanip>
///cout << fixed << setprecision(13) << (double) x << endl;
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
#define Mp(a, b) make_pair(a, b)
#define asd puts("asdasdasdasdasdf");
typedef long long ll;
typedef pair <int, int> pl;
//typedef __int64 LL;
const int inf = 0x3f3f3f3f;
const int N = 100010;
int a[N], c[N];
ll sum, avg;
vector <pl> v;
int n;
void OUT()
{
puts("YES");
int sz = v.size();
printf("%d\n", sz);
for( int i = 0; i < sz; ++i ) {
printf("%d %d\n", v[i].first, v[i].second );
}
}
int f()
{
for( int i = 2; i <= n; ++i ) {
int j = i+1;
if( j == n+1 )
j = 1;
if( abs( c[i]-avg ) > 1 )
return 0;
if( c[i] < avg ) {
c[i]++, c[j]--;
v.push_back( Mp( j, i ) );
}
else if( c[i] > avg ) {
c[i]--, c[j]++;
v.push_back( Mp( i, j ) );
}
}
for( int i = 1; i <= n; ++i )
if( c[i] != avg )
return 0;
return 1;
}
int main()
{
int tot;
scanf("%d", &tot);
while( tot-- ) {
scanf("%d", &n);
sum = 0;
for( int i = 1; i <= n; ++i ) {
scanf("%d", &a[i]);
sum += a[i];
}
if (sum % n) {
printf("NO\n");
continue;
}
if (n == 1) {
printf("YES\n");
printf("0\n");
continue;
}
avg = sum / n;
bool OK = 0;
int p;
for( int i = 0; i < 3; ++i ) {
v.clear();
memcpy( c, a, sizeof c );
if( i == 1 ) {
c[1]--, c[2]++;
v.push_back( Mp(1, 2) );
}
else if( i == 2 ) {
c[1]++, c[2]--;
v.push_back( Mp( 2, 1 ) );
}
OK = f();
if( OK ) {
OUT();
break;
}
}
if( OK == 0 )
puts("NO");
}
return 0;
}
wa:(保证其他地方都一样),然后wa3、 40+发
avg = sum / n;
int OK = 0;
v.clear();
memcpy( c, a, sizeof c );
c[1]++, c[2]--;
v.push_back( Mp( 2, 1 ) );
OK = f();
if( OK ) {
OUT();
continue;
}
v.clear();
memcpy( c, a, sizeof c );
c[1]--, c[2]++;
v.push_back( Mp( 1, 2 ) );
OK = f();
if( OK ) {
OUT();
continue;
}
v.clear();
memcpy( c, a, sizeof c );
OK = f();
if( OK ) {
OUT();
continue;
}
if( !OK ) {
puts("NO");
}