3 6 1 0 1 0 0 0 5 1 1 1 1 1 3 1 2 3
NO YES 0 YES 2 2 1 3 2
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <bitset> #include <algorithm> #include <climits> using namespace std; #define ls 2*i #define rs 2*i+1 #define UP(i,x,y) for(i=x;i<=y;i++) #define DOWN(i,x,y) for(i=x;i>=y;i--) #define MEM(a,x) memset(a,x,sizeof(a)) #define W(a) while(a) #define gcd(a,b) __gcd(a,b) #define LL long long #define N 100005 #define INF 0x3f3f3f3f #define EXP 1e-8 #define rank rank1 const int mod = 1000000007; int t,n; int a[N],f[N][2];//f[i][0]=1代表向前移动一个,f[i][1]=1代表向后移动一个, LL sum; int main() { int i,j,k; scanf("%d",&t); while(t--) { scanf("%d",&n); sum = 0; MEM(f,0); for(i = 0; i<n; i++) { scanf("%d",&a[i]); sum+=a[i]; } if(sum%n) { printf("NO\n"); continue; } sum/=n; for(i = 0; i<n; i++) a[i]-=sum; for(i = 0; i<2*n; i++) { int x = i%n; int y = (i+1)%n; if(a[x]>0&&a[y]<=0&&!f[x][1])//x向后移动一个 { a[x]--; a[y]++; if(!f[y][0]) f[x][1] = 1;//y并没有向前移则可以向后移 else f[x][1] = f[y][0] = 0;//否则两两抵消 } else if(a[y]>=0&&a[x]<0&&!f[y][0]) { a[x]++; a[y]--; if(!f[x][1]) f[y][0] = 1; else f[x][1] = f[y][0] = 0; } } int flag = 1,cnt = 0; for(i = 0; i<n; i++) { if(a[i]) { flag = 0; break; } cnt+=f[i][0]+f[i][1]; } if(n==2&&cnt==2) flag = 0; if(flag) { printf("YES\n%d\n",cnt); for(i = 1; i<=n; i++) { int x = i-1; int y = i+1; if(x == 0) x = n; if(y == n+1) y = 1; if(f[i-1][0]) printf("%d %d\n",i,x); if(f[i-1][1]) printf("%d %d\n",i,y); } } else printf("NO\n"); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/libin56842/article/details/47336525