Average(模拟)
Average
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2756 Accepted Submission(s): 650 Special Judge
Problem Description
There are n soda sitting around a round table. soda are numbered from 1 to n and i-th soda is adjacent to (i+1)-th soda, 1-st soda is adjacent to n-th soda.
Each soda has some candies in their hand. And they want to make the number of candies the same by doing some taking and giving operations. More specifically, every two adjacent soda x and y can do one of the following operations only once: 1. x-th soda gives y-th soda a candy if he has one; 2. y-th soda gives x-th soda a candy if he has one; 3. they just do nothing.
Now you are to determine whether it is possible and give a sequence of operations.
Each soda has some candies in their hand. And they want to make the number of candies the same by doing some taking and giving operations. More specifically, every two adjacent soda x and y can do one of the following operations only once: 1. x-th soda gives y-th soda a candy if he has one; 2. y-th soda gives x-th soda a candy if he has one; 3. they just do nothing.
Now you are to determine whether it is possible and give a sequence of operations.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first contains an integer n (1≤n≤105), the number of soda. The next line contains n integers a1,a2,…,an (0≤ai≤109), where ai denotes the candy i-th soda has.
The first contains an integer n (1≤n≤105), the number of soda. The next line contains n integers a1,a2,…,an (0≤ai≤109), where ai denotes the candy i-th soda has.
Output
For each test case, output "YES" (without the quotes) if possible, otherwise output "NO" (without the quotes) in the first line. If possible, then the output an integer m (0≤m≤n) in the second line denoting the number of operations needed. Then each of the following m lines contain two integers x and y (1≤x,y≤n), which means that x-th soda gives y-th soda a candy.
Sample Input
3 6 1 0 1 0 0 0 5 1 1 1 1 1 3 1 2 3
Sample Output
NO YES 0 YES 2 2 1 3 2
********** 7 * Author :Running_Time 8 * Created Time :2015-8-7 8:51:48 9 * File Name :A.cpp 10 ************************************************/ 11 12 #include <cstdio> 13 #include <algorithm> 14 #include <iostream> 15 #include <sstream> 16 #include <cstring> 17 #include <cmath> 18 #include <string> 19 #include <vector> 20 #include <queue> 21 #include <deque> 22 #include <stack> 23 #include <list> 24 #include <map> 25 #include <set> 26 #include <bitset> 27 #include <cstdlib> 28 #include <ctime> 29 using namespace std; 30 31 #define lson l, mid, rt << 1 32 #define rson mid + 1, r, rt << 1 | 1 33 typedef long long ll; 34 const int MAXN = 1e5 + 10; 35 const int INF = 0x3f3f3f3f; 36 const int MOD = 1e9 + 7; 37 int a[MAXN]; 38 int x[MAXN]; 39 int n, tot; 40 ll ave; 41 42 bool work(void){ 43 tot = 0; 44 if (x[1] != 0) tot++; 45 if (x[n] != 0) tot++; 46 if (x[n-1] != 0) tot++; 47 for (int i=2; i<=n-2; ++i) { 48 if (abs (a[i] - ave + x[i-1]) > 1) return false; 49 x[i] = a[i] - ave + x[i-1]; 50 if (x[i] != 0) tot++; 51 } 52 if (a[n-1] - x[n-1] + x[n-2] != ave) return false; 53 return true; 54 } 55 56 bool judge(void){ 57 for (int i=-1; i<=1; ++i) { 58 for (int j=-1; j<=1; ++j) { 59 x[1] = i, x[n] = j; 60 if (abs (ave - a[n] + x[n]) <= 1) { 61 x[n-1] = ave - a[n] + x[n]; 62 if (work ()) return true; 63 } 64 } 65 } 66 return false; 67 } 68 69 int main(void) { //HDOJ 5353 Average 70 int T; scanf ("%d", &T); 71 while (T--) { 72 scanf ("%d", &n); ll sum = 0; 73 /* 74 for (int i=1; i<=n; ++i) { 75 scanf ("%d", &a[i]); sum += a[i]; 76 } 77 if (sum % n != 0) { 78 puts ("NO"); continue; 79 } 80 ave = sum / n; bool flag = true, same = true; 81 for (int i=1; i<=n; ++i) { 82 if (a[i] < ave - 2 || a[i] > ave + 2){ 83 flag = false; break; 84 } 85 if (a[i] != ave) same = false; 86 } 87 if (!flag) { 88 puts ("NO"); continue; 89 } 90 if (same) { 91 printf ("YES\n0\n"); continue; 92 } 93 */ 94 memset (x, 0, sizeof (x)); 95 if (!judge ()) { 96 puts ("NO"); continue; 97 } 98 puts ("YES"); printf ("%d\n", tot); 99 for (int i=1; i<=n; ++i) { 100 if (x[i] == 0) continue; 101 else if (x[i] == 1) printf ("%d %d\n", i, (i == n) ? 1 : i + 1); 102 else printf ("%d %d\n", (i == n) ? 1 : i + 1, i); 103 } 104 } 105 106 return 0; 107 }
题解:神奇的模拟,看了两天;x数组保存对于下一个值的操作;参考大神的写的,还是错。。。
代码:
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int INF=0x3f3f3f3f; const int MAXN=1e5+10; int tot,n; int x[MAXN],a[MAXN]; typedef long long LL; LL ave; bool work(){ tot=0; if(x[1]!=0)tot++; if(x[n]!=0)tot++; if(x[n-1]!=0)tot++; for(int i=2;i<=n-2;i++){ if(abs(a[i]-ave+x[i-1])>1)return false; x[i]=a[i]-ave+x[i-1]; if(x[i]!=0)tot++; } if(a[n-1]-x[n-1]+x[n-2]!=ave)return false; return true; } bool find(){ for(int i=-1;i<=1;i++){ for(int j=-1;j<=1;j++){ x[1]=i;x[n]=j; if(abs(ave-a[n]+x[n])<=1){ x[n-1]=ave-a[n]+x[n]; if(work())return true; } } } return false; } int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d",&n); memset(x,0,sizeof(x)); LL sum=0; for(int i=1;i<=n;i++)scanf("%d",a+i),sum+=a[i]; if(sum%n!=0){ puts("NO");continue; } ave=sum/n; int flot=0,same=1; for(int i=1;i<=n;i++){ if(abs(a[i]-ave)>=2){ puts("NO");flot=1;break; } if(a[i]!=same)same=0; } if(flot)continue; if(same){ puts("YES\n0");continue; } if(find()){ puts("YES"); } else{ puts("NO");continue; } printf("%d\n",tot); // for(int i=1;i<=n;i++)printf("%d ",x[i]);puts(""); for(int i=1;i<=n;i++){ if(!x[i])continue; if(x[i]==1)printf("%d %d\n",i,i==n?1:i+1); else printf("%d %d\n",i==n?1:i+1,i); } } return 0; }