[hdu5353]模拟
题意:有n个小朋友,每个小朋友手上有一些糖,考虑每两个相邻的小朋友a、b,可以选择执行3种操作中的任一种:(1)a给b一粒糖(2)b给a一粒糖(3)不进行任何动作,问能否通过确定每两个相邻的小朋友的操作使得最终每个人的糖果数量相等。
思路:如果只有1个小朋友,那么肯定是可行的,如果糖果数总和取模小朋友数不为0,那么肯定是不可行的。令第i个小朋友的糖果数为a[i],首先将平均值ave计算出来,然后a[i]=a[i]-ave。注意到a[i]的值是确定的,且每个a[i]到达的目标值也是确定的,也就是0,那么有一个显然的性质,如果一直a[i]给了a[i+1]x粒糖,那么相当于就知道了a[i+1]给了a[i+2]多少粒糖,因为要保证a[i+1]等于0,所以这个值是确定的,类似这样可以得到全部的a[i]给了a[i+1]多少粒糖(0表示没给,-1表示接受了对方给的1粒糖),同时如果中途出现某个a[i]不为0,则可以立即判定当前是无解的。于是只需枚举a[0]给了a[1]多少粒糖(只有-1,0,1三种),然后一边递推一边检查矛盾即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | #include <map> #include <set> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define X first #define Y second #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() #define fillchar(a, x) memset(a, x, sizeof(a)) typedef long long ll; typedef pair< int , int > pii; typedef unsigned long long ull; #ifndef ONLINE_JUDGE void RI(vector< int >&a, int n){a.resize(n); for ( int i=0;i<n;i++) scanf ( "%d" ,&a[i]);} void RI(){} void RI( int &X){ scanf ( "%d" ,&X);} template < typename ...R> void RI( int &f,R&...r){RI(f);RI(r...);} void RI( int *p, int *q){ int d=p<q?1:-1; while (p!=q){ scanf ( "%d" ,p);p+=d;}} void print(){cout<<endl;} template < typename T> void print( const T t){cout<<t<<endl;} template < typename F, typename ...R> void print( const F f, const R...r){cout<<f<< ", " ;print(r...);} template < typename T> void print(T*p, T*q){ int d=p<q?1:-1; while (p!=q){cout<<*p<< ", " ;p+=d;}cout<<endl;} #endif template < typename T> bool umax(T&a, const T&b){ return b<=a? false :(a=b, true );} template < typename T> bool umin(T&a, const T&b){ return b>=a? false :(a=b, true );} template < typename T> void V2A(T a[], const vector<T>&b){ for ( int i=0;i<b.size();i++)a[i]=b[i];} template < typename T> void A2V(vector<T>&a, const T b[]){ for ( int i=0;i<a.size();i++)a[i]=b[i];} const double PI = acos (-1.0); const int INF = 1e9 + 7; /* -------------------------------------------------------------------------------- */ const int maxn = 1e5 + 7; int n, m; pii ans[maxn]; int a[maxn], b[maxn]; bool chk( int x) { memcpy (b, a, sizeof (a)); b[0] -= x; b[1] += x; m = 0; if (x == 1) ans[m ++] = mp(0, 1); if (x == -1) ans[m ++] = mp(1, 0); for ( int i = 1; i < n; i ++) { int v = (i + 1) % n; if ( abs (b[i]) > 1) return false ; if (b[i] == 1) { b[v] ++; ans[m ++] = mp(i, v); } if (b[i] == -1) { b[v] --; ans[m ++] = mp(v, i); } } return true ; } bool work() { if ( abs (a[0]) > 2) return false ; if (chk(1) || chk(0) || chk(-1)) return true ; return false ; } bool all_zero() { for ( int i = 0; i < n; i ++) { if (b[i]) return false ; } return true ; } int main() { #ifndef ONLINE_JUDGE freopen ( "in.txt" , "r" , stdin); //freopen("out.txt", "w", stdout); #endif // ONLINE_JUDGE int T; cin >> T; while (T --) { cin >> n; ll sum = 0; for ( int i = 0; i < n; i ++) { scanf ( "%d" , a + i); b[i] = a[i]; sum += a[i]; } if (sum % n) puts ( "NO" ); else { if (n == 1 || all_zero()) { puts ( "YES" ); puts ( "0" ); continue ; } int ave = sum / n; for ( int i = 0; i < n; i ++) a[i] -= ave; m = 0; if (work()) { puts ( "YES" ); printf ( "%d\n" , m); for ( int i = 0; i < m; i ++) printf ( "%d %d\n" , ans[i].X + 1, ans[i].Y + 1); } else puts ( "NO" ); } } return 0; } |