就是贪心啊,不知道为啥总是不过,总是WA
方法不对吗?
将数组扩展一倍,从左到右扫描,大于平均数就给右边的,小于就从右边拿,等于就不变,记录下操作类型。
大于2直接NO,应该是正确的算法吧,自己出了一些数据也都过了
1 #pragma comment(linker, "/STACK:102400000,102400000") 2 #include <iostream> 3 #include <cstdio> 4 #include <fstream> 5 #include <algorithm> 6 #include <cmath> 7 #include <deque> 8 #include <vector> 9 #include <queue> 10 #include <string> 11 #include <cstring> 12 #include <map> 13 #include <stack> 14 #include <set> 15 #define LL long long 16 #define INF 0x3f3f3f3f 17 #define MAXN 200005 18 using namespace std; 19 struct Node 20 { 21 int x, y; 22 Node(int x = 0, int y = 0):x(x), y(y){}; 23 }; 24 vector<Node> res; 25 int n; 26 LL sum; 27 LL a[MAXN]; 28 int f[MAXN]; 29 int main() 30 { 31 #ifndef ONLINE_JUDGE 32 freopen("in.txt", "r", stdin); 33 //freopen("out.txt", "w", stdout); 34 #endif // OPEN_FILE 35 int T; 36 scanf("%d", &T); 37 for(int cas = 1; cas <= T; cas++){ 38 scanf("%d", &n); 39 sum = 0; 40 for(int i = 1; i <= n; i++){ 41 scanf("%d", &a[i]); 42 a[i + n] = a[i]; 43 sum += a[i]; 44 } 45 if(sum % n != 0){ 46 printf("NO\n"); 47 continue; 48 } 49 LL ave = sum / n; 50 memset(f, 0, sizeof(f)); 51 bool noans = false; 52 for(int i = 1; i < 2 * n; i++){ 53 if(a[i] == ave){ 54 continue; 55 } 56 if(a[i] < ave){ 57 if(ave - a[i] > 2){ 58 noans = true; 59 break; 60 } 61 a[i]++; 62 a[i + 1]--; 63 f[i] = 1; 64 continue; 65 } 66 if(a[i] > ave){ 67 if(a[i] - ave > 2){ 68 noans = true; 69 break; 70 } 71 a[i]--; 72 a[i + 1]++; 73 f[i] = -1; 74 } 75 } 76 if(noans){ 77 printf("NO\n"); 78 continue; 79 } 80 noans = true; 81 int cnt = 1; 82 int pos = 0; 83 for(int i = 2; i <= 2 * n; i++){ 84 if(a[i] == a[i - 1]){ 85 cnt++; 86 } 87 else{ 88 cnt = 1; 89 } 90 if(cnt == n){ 91 noans = false; 92 pos = i; 93 break; 94 } 95 } 96 if(noans){ 97 printf("NO\n"); 98 continue; 99 } 100 printf("YES\n"); 101 int x, y; 102 res.clear(); 103 for(int i = pos - n + 1; i <= pos; i++){ 104 if(f[i] == 0){ 105 continue; 106 } 107 if(f[i] == 1){ 108 x = i + 1; 109 y = i; 110 } 111 else{ 112 x = i; 113 y = i + 1; 114 } 115 if(x > n){ 116 x -= n; 117 } 118 if(y > n){ 119 y -= n; 120 } 121 res.push_back(Node(x, y)); 122 } 123 printf("%d\n", res.size()); 124 for(int i = 0; i < res.size(); i++){ 125 printf("%d %d\n", res[i].x, res[i].y); 126 } 127 } 128 }
时间: 2024-10-14 00:37:21