#include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; /*#include <atcoder/modint> using namespace atcoder; using mint = modint998244353; vector <mint> fac, inv; mint binom(int n, int k) { if (n < k || n < 0) return 0; return fac[n] * inv[k] * inv[n-k]; } void prep(int N) { fac.resize(N+1, 1); inv.resize(N+1, 1); for (int i=1; i<=N; ++i) fac[i] = fac[i-1] * i; inv[N] = fac[N].inv(); for (int i=N-1; i>0; --i) inv[i] = inv[i+1] * (i + 1); }*/ mt19937_64 rng(time(0)); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif const int maxn = 1000100; //Did you REAALLY consider sample tests? vi infect_cities(vi cities, vi &safe) { vi result = cities; int n = sz(cities); rep(i, 0, n) { if (safe[i]) continue; if (i > 0) result[i] |= cities[i - 1]; if (i + 1 < n) result[i] |= cities[i + 1]; } return result; } int brut(vi vec) { int n = sz(vec); vi perm(n); rep(i, 0, n) perm[i] = i; int best_result = INT_MAX; do { vi safe(n, 0); vi cities = vec; trav(vaccinating, perm) { if (!cities[vaccinating]) safe[vaccinating] = 1; cities = infect_cities(cities, safe); } int option = 0; rep(i, 0, n) option += cities[i]; best_result = min(best_result, option); }while(next_permutation(all(perm))); return best_result; } vi compress_intervals(vi vec) { vec.pb(1); reverse(all(vec)); vec.pb(1); reverse(all(vec)); vector <int> zeroes; rep(i, 1, sz(vec)) { if (vec[i] == 0) { if (vec[i-1] == 1) zeroes.pb(1); else zeroes.back() += 1; } } return zeroes; } int wzo(vi vec) { auto sequences = compress_intervals(vec); int n = sz(vec); vi dwojki; vi jedynki; if (sequences.empty()) return n; //all virus if (sequences[0] == n) return 0; //no virus int sajz = sz(sequences); for (int i = 1; i + 1 < sajz; ++i) { dwojki.pb(sequences[i]); } if (sz(sequences) == 1) { int border = 0; if (vec[0] == 1) ++border; if (vec.back() == 1) ++border; assert(border > 0); if (border == 1) jedynki.pb(sequences[0]); else dwojki.pb(sequences[0]); } else { if (vec[0] == 1) dwojki.pb(sequences[0]); else jedynki.pb(sequences[0]); if (vec.back() == 1) dwojki.pb(sequences[sajz - 1]); else jedynki.pb(sequences[sajz - 1]); } sort(all(jedynki), greater<int>()); sort(all(dwojki), greater<int>()); vi cpdwojki = dwojki; assert(sz(jedynki) <= 2); //debug(dwojki); debug(jedynki); // may be the case that some ones are ignored completely int best = 0; for (int used_ones = 0; used_ones <= sz(jedynki); ++used_ones) { dwojki = cpdwojki; int time_passed = 0; int score = 0; for (int i=0; i<used_ones; ++i) { score += max(0, jedynki[i] - time_passed); time_passed += 1; } for (int i=0; i<sz(dwojki); ++i) { int removed_so_far = 2 * time_passed; if (removed_so_far >= dwojki[i]) break; dwojki[i] -= removed_so_far; assert(dwojki[i] > 0); score += 1; dwojki[i] -= 2; //from both ends disappear time_passed += 1; if (dwojki[i] > 0) { time_passed += 1; score += dwojki[i]; } } best = max(best, score); } return n - best; } bool TEST = 0; int main() { boost; int tests; cin >> tests; while (tests--) { int n; string s; if (!TEST) { cin >> n >> s; } else { //TEST GENERATION const int prob_zero = 80; n = random(1, 8); s = ""; rep(i, 0, n) { if (random(0, 100) < prob_zero) s += '0'; else s += '1'; } //END TEST GENERATION } vi vec(n); rep(i, 0, n) vec[i] = (s[i] == '1') ? 1 : 0; if (!TEST) { cout << wzo(vec) << "\n"; } else { if (tests % 100 == 0) cout << tests << endl; auto model = wzo(vec); auto slow = brut(vec); if (model != slow) { debug(vec); debug(model); debug(slow); OUT("WA"); } } } }
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; /*#include <atcoder/modint> using namespace atcoder; using mint = modint998244353; vector <mint> fac, inv; mint binom(int n, int k) { if (n < k || n < 0) return 0; return fac[n] * inv[k] * inv[n-k]; } void prep(int N) { fac.resize(N+1, 1); inv.resize(N+1, 1); for (int i=1; i<=N; ++i) fac[i] = fac[i-1] * i; inv[N] = fac[N].inv(); for (int i=N-1; i>0; --i) inv[i] = inv[i+1] * (i + 1); }*/ mt19937_64 rng(time(0)); int random(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif const int maxn = 1000100; //Did you REAALLY consider sample tests? vi infect_cities(vi cities, vi &safe) { vi result = cities; int n = sz(cities); rep(i, 0, n) { if (safe[i]) continue; if (i > 0) result[i] |= cities[i - 1]; if (i + 1 < n) result[i] |= cities[i + 1]; } return result; } int brut(vi vec) { int n = sz(vec); vi perm(n); rep(i, 0, n) perm[i] = i; int best_result = INT_MAX; do { vi safe(n, 0); vi cities = vec; trav(vaccinating, perm) { if (!cities[vaccinating]) safe[vaccinating] = 1; cities = infect_cities(cities, safe); } int option = 0; rep(i, 0, n) option += cities[i]; best_result = min(best_result, option); }while(next_permutation(all(perm))); return best_result; } vi compress_intervals(vi vec) { vec.pb(1); reverse(all(vec)); vec.pb(1); reverse(all(vec)); vector <int> zeroes; rep(i, 1, sz(vec)) { if (vec[i] == 0) { if (vec[i-1] == 1) zeroes.pb(1); else zeroes.back() += 1; } } return zeroes; } int wzo(vi vec) { auto sequences = compress_intervals(vec); int n = sz(vec); vi dwojki; vi jedynki; if (sequences.empty()) return n; //all virus if (sequences[0] == n) return 0; //no virus int sajz = sz(sequences); for (int i = 1; i + 1 < sajz; ++i) { dwojki.pb(sequences[i]); } if (sz(sequences) == 1) { int border = 0; if (vec[0] == 1) ++border; if (vec.back() == 1) ++border; assert(border > 0); if (border == 1) jedynki.pb(sequences[0]); else dwojki.pb(sequences[0]); } else { if (vec[0] == 1) dwojki.pb(sequences[0]); else jedynki.pb(sequences[0]); if (vec.back() == 1) dwojki.pb(sequences[sajz - 1]); else jedynki.pb(sequences[sajz - 1]); } sort(all(jedynki), greater<int>()); sort(all(dwojki), greater<int>()); vi cpdwojki = dwojki; assert(sz(jedynki) <= 2); //debug(dwojki); debug(jedynki); // may be the case that some ones are ignored completely int best = 0; for (int used_ones = 0; used_ones <= sz(jedynki); ++used_ones) { dwojki = cpdwojki; int time_passed = 0; int score = 0; for (int i=0; i<used_ones; ++i) { score += max(0, jedynki[i] - time_passed); time_passed += 1; } for (int i=0; i<sz(dwojki); ++i) { int removed_so_far = 2 * time_passed; if (removed_so_far >= dwojki[i]) break; dwojki[i] -= removed_so_far; assert(dwojki[i] > 0); score += 1; dwojki[i] -= 2; //from both ends disappear time_passed += 1; if (dwojki[i] > 0) { time_passed += 1; score += dwojki[i]; } } best = max(best, score); } return n - best; } bool TEST = 0; int main() { boost; int tests; cin >> tests; while (tests--) { int n; string s; if (!TEST) { cin >> n >> s; } else { //TEST GENERATION const int prob_zero = 80; n = random(1, 8); s = ""; rep(i, 0, n) { if (random(0, 100) < prob_zero) s += '0'; else s += '1'; } //END TEST GENERATION } vi vec(n); rep(i, 0, n) vec[i] = (s[i] == '1') ? 1 : 0; if (!TEST) { cout << wzo(vec) << "\n"; } else { if (tests % 100 == 0) cout << tests << endl; auto model = wzo(vec); auto slow = brut(vec); if (model != slow) { debug(vec); debug(model); debug(slow); OUT("WA"); } } } } |