#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1000000000000000; int f(int k, int c[]) { if (k == 1) { return c[0]; } int a[k]; for (int i = 0; i < k; i++) { a[i] = c[i]; } sort(a + 1, a + k - 1, greater<int>()); int px = 1; while ((4 * px - 3 < a[px]) && (px < k - 1)) { px++; } if (4 * px - 3 >= a[px]) { px--; } int resx = 0; for (int i = 1; i <= px; i++) { resx += a[i]; } resx -= px * (2 * px - 1); int L = 4 * px + 1; bool b = 0; for (int i = 1; i < k - 1; i++) { if (a[i] == L) { b = 1; } } if (b) { resx++; } int py = 2; while ((4 * py - 5 < a[py - 1]) && (py - 1 < k - 1)) { py++; } if (4 * py - 5 >= a[py - 1]) { py--; } int resy = 0; for (int i = 1; i <= py - 1; i++) { resy += a[i]; } resy -= (py - 1) * (2 * py - 1); resy += max(a[0], a[k - 1]); L = 4 * py - 1; b = 0; for (int i = 1; i < k - 1; i++) { if (a[i] == L) { b = 1; } } if (b) { resy++; } int pz = 3; while ((4 * pz - 7 < a[pz - 2]) && (pz - 2 < k - 1)) { pz++; } if (4 * pz - 7 >= a[pz - 2]) { pz--; } int resz = 0; for (int i = 1; i <= pz - 2; i++) { resz += a[i]; } resz -= (pz - 1) * (2 * pz - 3); resz += a[0] + a[k - 1]; L = 4 * pz - 3; b = 0; for (int i = 1; i < k - 1; i++) { if (a[i] == L) { b = 1; } } if (b) { resz++; } int res = max(max(resx, resy), resz); return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; int res[t + 1]; for (int i = 1; i <= t; i++) { int n, k = 0; bool b = 1; cin >> n; char x[n + 1]; for (int j = 1; j <= n; j++) { cin >> x[j]; if (b && x[j] == '0') { k++; b = 0; } else if (x[j] == '1') { b = 1; } } if (x[1] == '1') { k++; } if (x[n] == '1') { k++; } int ind = 0; int a[k]; for (int j = 0; j < k; j++) { a[j] = 0; } for (int j = 1; j <= n; j++) { if (x[j] == '0') { if (b) { ind++; } a[ind]++; b = 0; } else { b = 1; } } res[i] = n - f(k, a); } for (int i = 1; i <= t; i++) { cout << res[i] << "\n"; } return 0; }
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1000000000000000; int f(int k, int c[]) { if (k == 1) { return c[0]; } int a[k]; for (int i = 0; i < k; i++) { a[i] = c[i]; } sort(a + 1, a + k - 1, greater<int>()); int px = 1; while ((4 * px - 3 < a[px]) && (px < k - 1)) { px++; } if (4 * px - 3 >= a[px]) { px--; } int resx = 0; for (int i = 1; i <= px; i++) { resx += a[i]; } resx -= px * (2 * px - 1); int L = 4 * px + 1; bool b = 0; for (int i = 1; i < k - 1; i++) { if (a[i] == L) { b = 1; } } if (b) { resx++; } int py = 2; while ((4 * py - 5 < a[py - 1]) && (py - 1 < k - 1)) { py++; } if (4 * py - 5 >= a[py - 1]) { py--; } int resy = 0; for (int i = 1; i <= py - 1; i++) { resy += a[i]; } resy -= (py - 1) * (2 * py - 1); resy += max(a[0], a[k - 1]); L = 4 * py - 1; b = 0; for (int i = 1; i < k - 1; i++) { if (a[i] == L) { b = 1; } } if (b) { resy++; } int pz = 3; while ((4 * pz - 7 < a[pz - 2]) && (pz - 2 < k - 1)) { pz++; } if (4 * pz - 7 >= a[pz - 2]) { pz--; } int resz = 0; for (int i = 1; i <= pz - 2; i++) { resz += a[i]; } resz -= (pz - 1) * (2 * pz - 3); resz += a[0] + a[k - 1]; L = 4 * pz - 3; b = 0; for (int i = 1; i < k - 1; i++) { if (a[i] == L) { b = 1; } } if (b) { resz++; } int res = max(max(resx, resy), resz); return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; int res[t + 1]; for (int i = 1; i <= t; i++) { int n, k = 0; bool b = 1; cin >> n; char x[n + 1]; for (int j = 1; j <= n; j++) { cin >> x[j]; if (b && x[j] == '0') { k++; b = 0; } else if (x[j] == '1') { b = 1; } } if (x[1] == '1') { k++; } if (x[n] == '1') { k++; } int ind = 0; int a[k]; for (int j = 0; j < k; j++) { a[j] = 0; } for (int j = 1; j <= n; j++) { if (x[j] == '0') { if (b) { ind++; } a[ind]++; b = 0; } else { b = 1; } } res[i] = n - f(k, a); } for (int i = 1; i <= t; i++) { cout << res[i] << "\n"; } return 0; } |