#include <bits/stdc++.h> #define PB push_back #define ST first #define ND second //#pragma GCC optimize ("O3") //#pragma GCC target("tune=native") //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //using namespace __gnu_pbds; //typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; using namespace std; using ll = long long; using pi = pair<int, int>; using vi = vector<int>; int t, n; const int nax = 1e5 + 10; string s; int oneside[5]; int r[nax]; int dp[nax][5]; void solve() { cin >> n >> s; int any = 0; for(auto c : s) any |= ('1' == c); if(!any) { cout << "0\n"; return; } int g = 0; any = 0; int ptr = 0; for(int i = 0; i < n; ++i) { r[i] = 0; } oneside[0] = oneside[1] = 0; for(int i = 0; i < n; ++i) { if(s[i] == '0') { g++; } else { if(!any) { oneside[0] = g; } else { r[ptr++] = g; } g = 0; any = 1; } } oneside[1] = g; sort(r, r + n); reverse(r, r + n); int ans = 0; for(int i = 1; i <= n; ++i) { // poczatek i-tego dnia for(int mask = 0; mask < (1 << 2); ++mask) { // maska odwiedzonych oneside dp[i][mask] = dp[i - 1][mask]; int cnt = 0; for(int j = 0; j < 2; ++j) { if((1 << j) & mask) { dp[i][mask] = max(dp[i][mask], dp[i - 1][mask ^ (1 << j)] + oneside[j] - (i-1)); cnt++; } } //nie biore z maski, to int id = (i - cnt - 1) / 2; if(id >= 0 && (i - cnt - 1) % 2 == 1) { if(i >= 2) { dp[i][mask] = max(dp[i][mask], dp[i - 2][mask] + r[id] - 2*(i-2) - 1); } } if(id >= 0 && r[id] > 2 * (i - 1) && (i - cnt - 1)%2==0) { ans = max(ans, dp[i - 1][mask] + 1); } //cout << i << " " << mask << ": " << dp[i][mask] << "\n"; } } for(int mask = 0; mask < (1 << 2); ++mask) { ans = max(ans, dp[n][mask]); } cout << n - ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> t; while(t--) { solve(); } }
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 | #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second //#pragma GCC optimize ("O3") //#pragma GCC target("tune=native") //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //using namespace __gnu_pbds; //typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; using namespace std; using ll = long long; using pi = pair<int, int>; using vi = vector<int>; int t, n; const int nax = 1e5 + 10; string s; int oneside[5]; int r[nax]; int dp[nax][5]; void solve() { cin >> n >> s; int any = 0; for(auto c : s) any |= ('1' == c); if(!any) { cout << "0\n"; return; } int g = 0; any = 0; int ptr = 0; for(int i = 0; i < n; ++i) { r[i] = 0; } oneside[0] = oneside[1] = 0; for(int i = 0; i < n; ++i) { if(s[i] == '0') { g++; } else { if(!any) { oneside[0] = g; } else { r[ptr++] = g; } g = 0; any = 1; } } oneside[1] = g; sort(r, r + n); reverse(r, r + n); int ans = 0; for(int i = 1; i <= n; ++i) { // poczatek i-tego dnia for(int mask = 0; mask < (1 << 2); ++mask) { // maska odwiedzonych oneside dp[i][mask] = dp[i - 1][mask]; int cnt = 0; for(int j = 0; j < 2; ++j) { if((1 << j) & mask) { dp[i][mask] = max(dp[i][mask], dp[i - 1][mask ^ (1 << j)] + oneside[j] - (i-1)); cnt++; } } //nie biore z maski, to int id = (i - cnt - 1) / 2; if(id >= 0 && (i - cnt - 1) % 2 == 1) { if(i >= 2) { dp[i][mask] = max(dp[i][mask], dp[i - 2][mask] + r[id] - 2*(i-2) - 1); } } if(id >= 0 && r[id] > 2 * (i - 1) && (i - cnt - 1)%2==0) { ans = max(ans, dp[i - 1][mask] + 1); } //cout << i << " " << mask << ": " << dp[i][mask] << "\n"; } } for(int mask = 0; mask < (1 << 2); ++mask) { ans = max(ans, dp[n][mask]); } cout << n - ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> t; while(t--) { solve(); } } |