#include<bits/stdc++.h> #define maxn 100010 using namespace std; vector<int> v[2]; int dp[maxn][3]; void dodaj(int j, int last, bool czy) { int x = j - last - 1; if (x != 0) { if (last == -1 || czy) { v[0].push_back(x); } else { v[1].push_back(x); } } } bool por (int a, int b) { return a > b; } void calculate0(int i) { if (v[1][i-1] - 4*(i-1) == 1) { dp[i][0] = dp[i-1][0] + 1; } else { dp[i][0] = dp[i-1][0] + v[1][i-1] - 4*(i-1) - 1; } } void calculate1(int i) { int x = dp[i-1][1]; if ((int)v[0].size() == 0) { dp[i][1] = x; return; } //pierwszy ostatni if (v[0][0] > 2 * i) { x = max(x, dp[i][0] + v[0][0] - 2 * i); } //i-ty ostatni if (v[1][i-1] > 4 * (i-1)+2) { if (v[1][i-1] - (4 * (i-1)+2) != 1) x = max(x, dp[i-1][1] + v[1][i-1] - (4 * (i-1)+2)-1); else x = max(x, dp[i-1][1] + 1); } dp[i][1] = x; } void calculate2(int i) { int x = dp[i-1][2]; if ((int)v[0].size() < 2) { dp[i][2] = x; return; } //drugi ostatni if (v[0][1] > 2 * i + 1) { x = max(x, dp[i][1] + v[0][1] - 2 * i - 1); } //i-ty ostatni if (v[1][i-1] > 4 * (i-1)+4) { if (v[1][i-1] - (4 * (i-1)+4) != 1) x = max(x, dp[i-1][2] + v[1][i-1] - (4 * (i-1)+4)-1); else x = max(x, dp[i-1][2] + 1); } dp[i][2] = x; } int make_dp() { if ((int)v[0].size() > 0) { dp[0][0] = 0; dp[0][1] = v[0][0]; if ((int)v[0].size() > 1) dp[0][2] = v[0][0] + v[0][1] - 1; else dp[0][2] = 0; } else { dp[0][0] = dp[0][1] = dp[0][2] = 0; } for (int i = 1; i <= (int)v[1].size(); ++i) { if (v[1][i-1] <= 4 *(i-1)) { return max(dp[i-1][0], max(dp[i-1][1], dp[i-1][2])); } calculate0(i); calculate1(i); calculate2(i); //cout << dp[i][0] << " " << dp[i][1] << " " << dp[i][2] << endl; } int n = (int)v[1].size(); //cout << n << endl; return max(dp[n][0], max(dp[n][1], dp[n][2])); } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int t,n; string a; cin >> t; for (int i = 0; i < t; ++i) { cin >> n; cin >> a; int last = -1; bool check = false; for (int j = 0; j < n; ++j) { if (a[j] == '1') { check = true; dodaj(j, last, false); last = j; } } if (last != n - 1) dodaj(n, last, true); if ((int)v[0].size() != 0) sort(v[0].begin(), v[0].end(), por); if ((int)v[1].size() != 0) sort(v[1].begin(), v[1].end(), por); if (!check) { cout << "0\n"; } else { //cout << make_dp() << endl; cout << n - make_dp() << "\n"; } v[0].clear(); v[1].clear(); } }
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 | #include<bits/stdc++.h> #define maxn 100010 using namespace std; vector<int> v[2]; int dp[maxn][3]; void dodaj(int j, int last, bool czy) { int x = j - last - 1; if (x != 0) { if (last == -1 || czy) { v[0].push_back(x); } else { v[1].push_back(x); } } } bool por (int a, int b) { return a > b; } void calculate0(int i) { if (v[1][i-1] - 4*(i-1) == 1) { dp[i][0] = dp[i-1][0] + 1; } else { dp[i][0] = dp[i-1][0] + v[1][i-1] - 4*(i-1) - 1; } } void calculate1(int i) { int x = dp[i-1][1]; if ((int)v[0].size() == 0) { dp[i][1] = x; return; } //pierwszy ostatni if (v[0][0] > 2 * i) { x = max(x, dp[i][0] + v[0][0] - 2 * i); } //i-ty ostatni if (v[1][i-1] > 4 * (i-1)+2) { if (v[1][i-1] - (4 * (i-1)+2) != 1) x = max(x, dp[i-1][1] + v[1][i-1] - (4 * (i-1)+2)-1); else x = max(x, dp[i-1][1] + 1); } dp[i][1] = x; } void calculate2(int i) { int x = dp[i-1][2]; if ((int)v[0].size() < 2) { dp[i][2] = x; return; } //drugi ostatni if (v[0][1] > 2 * i + 1) { x = max(x, dp[i][1] + v[0][1] - 2 * i - 1); } //i-ty ostatni if (v[1][i-1] > 4 * (i-1)+4) { if (v[1][i-1] - (4 * (i-1)+4) != 1) x = max(x, dp[i-1][2] + v[1][i-1] - (4 * (i-1)+4)-1); else x = max(x, dp[i-1][2] + 1); } dp[i][2] = x; } int make_dp() { if ((int)v[0].size() > 0) { dp[0][0] = 0; dp[0][1] = v[0][0]; if ((int)v[0].size() > 1) dp[0][2] = v[0][0] + v[0][1] - 1; else dp[0][2] = 0; } else { dp[0][0] = dp[0][1] = dp[0][2] = 0; } for (int i = 1; i <= (int)v[1].size(); ++i) { if (v[1][i-1] <= 4 *(i-1)) { return max(dp[i-1][0], max(dp[i-1][1], dp[i-1][2])); } calculate0(i); calculate1(i); calculate2(i); //cout << dp[i][0] << " " << dp[i][1] << " " << dp[i][2] << endl; } int n = (int)v[1].size(); //cout << n << endl; return max(dp[n][0], max(dp[n][1], dp[n][2])); } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int t,n; string a; cin >> t; for (int i = 0; i < t; ++i) { cin >> n; cin >> a; int last = -1; bool check = false; for (int j = 0; j < n; ++j) { if (a[j] == '1') { check = true; dodaj(j, last, false); last = j; } } if (last != n - 1) dodaj(n, last, true); if ((int)v[0].size() != 0) sort(v[0].begin(), v[0].end(), por); if ((int)v[1].size() != 0) sort(v[1].begin(), v[1].end(), por); if (!check) { cout << "0\n"; } else { //cout << make_dp() << endl; cout << n - make_dp() << "\n"; } v[0].clear(); v[1].clear(); } } |