#include <algorithm> #include <map> #include <cstdio> using namespace std; void reduce(map<int, int>* prev, map<int, int>* next, int delta) { for(auto it = prev->begin(); it != prev->end(); it++) { if((it->first) > delta) { (*next)[(it->first) - delta] = it->second; } } } struct State { map<int, int>* ones; map<int, int>* twos; State(): ones(new map<int, int>), twos(new map<int, int>) {} State(char* a, int n): State() { int start = 0; for(int i=1;i<n;i++) { if((a[i] == '1') && (a[i-1] != '1')) { if(start == 0) { (*ones)[i]++; } else { (*twos)[i-start]++; } } else if((a[i] != '1') && (a[i-1] == '1')) { start = i; } } if(a[n-1] != '1') { (*ones)[n-start]++; } } int countOnes() { int res = 0; for(auto it = ones->begin();it != ones->end();it++) { res += it->second; } return res; } State* withoutOne(int k) { State* next = new State(); map<int, int>* newOnes = new map<int, int>; map<int, int>* newTwos = new map<int, int>; reduce(ones, newOnes, 1); if((*(newOnes))[k-1] > 1) { (*(newOnes))[k-1]--; } else { newOnes->erase(k-1); } reduce(twos, newTwos, 2); next->ones = newOnes; next->twos = newTwos; return next; } }; pair<int, int> countZeros(int start, int t) { int remaining = start - t; if(remaining > 0) { return make_pair(remaining, 1); } return make_pair(0, 0); } pair<int, int> countZeros2(int start, int t) { int remaining = start - t; if((remaining == 1) || (remaining == 2)) { return make_pair(1, 1); } else if(remaining > 2) { return make_pair(remaining - 1, 2); } return make_pair(0, 0); } void dec(map<int, int>& m, int k) { if(m[k] > 1) { m[k]--; } else { m.erase(k); } } int solveStep(State* state) { int zeros = 0; int t = 0; map<int, int> ones(*(state->ones)); map<int, int> twos(*(state->twos)); while((!ones.empty()) || (!twos.empty())) { int last1 = -1; int last2 = -1; pair<int, int> s1; pair<int, int> s2; if(!ones.empty()) { last1 = ones.rbegin()->first; s1 = countZeros(last1, t); } if(!twos.empty()) { last2 = twos.rbegin()->first; s2 = countZeros2(last2, 2*t); } if((last1 >= 0) && (last2 >= 0)) { if(s1.first >= s2.first) { last2 = -1; } else { last1 = -1; } } if(last1 >= 0) { dec(ones, last1); zeros += s1.first; t += s1.second; } else { dec(twos, last2); zeros += s2.first; t += s2.second; } } return zeros; } int solve(State* state) { int full = solveStep(state); if(state->countOnes() <= 1) { return full; } int k = state->ones->rbegin()->first; State* r = state->withoutOne(k); int withoutOne = k + solveStep(r); delete r; return max(full, withoutOne); } int solve(char* a, int n) { bool oneExists = false; for(int i=0;i<n;i++) { if(a[i] == '1') { oneExists = true; break; } } if(!oneExists) { return 0; } State* state = new State(a, n); int res = n - solve(state); delete state; return res; } int main(int argc, char** argv) { int t, n; char a[100005]; scanf("%d", &t); for(int i=0;i<t;i++) { scanf("%d", &n); while(true) { char c = getchar(); if((c >= '0') && (c <= '1')) { a[0] = c; break; } } for(int j=1;j<n;j++) { a[j] = getchar(); } int res = solve(a, n); printf("%d\n", res); } 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | #include <algorithm> #include <map> #include <cstdio> using namespace std; void reduce(map<int, int>* prev, map<int, int>* next, int delta) { for(auto it = prev->begin(); it != prev->end(); it++) { if((it->first) > delta) { (*next)[(it->first) - delta] = it->second; } } } struct State { map<int, int>* ones; map<int, int>* twos; State(): ones(new map<int, int>), twos(new map<int, int>) {} State(char* a, int n): State() { int start = 0; for(int i=1;i<n;i++) { if((a[i] == '1') && (a[i-1] != '1')) { if(start == 0) { (*ones)[i]++; } else { (*twos)[i-start]++; } } else if((a[i] != '1') && (a[i-1] == '1')) { start = i; } } if(a[n-1] != '1') { (*ones)[n-start]++; } } int countOnes() { int res = 0; for(auto it = ones->begin();it != ones->end();it++) { res += it->second; } return res; } State* withoutOne(int k) { State* next = new State(); map<int, int>* newOnes = new map<int, int>; map<int, int>* newTwos = new map<int, int>; reduce(ones, newOnes, 1); if((*(newOnes))[k-1] > 1) { (*(newOnes))[k-1]--; } else { newOnes->erase(k-1); } reduce(twos, newTwos, 2); next->ones = newOnes; next->twos = newTwos; return next; } }; pair<int, int> countZeros(int start, int t) { int remaining = start - t; if(remaining > 0) { return make_pair(remaining, 1); } return make_pair(0, 0); } pair<int, int> countZeros2(int start, int t) { int remaining = start - t; if((remaining == 1) || (remaining == 2)) { return make_pair(1, 1); } else if(remaining > 2) { return make_pair(remaining - 1, 2); } return make_pair(0, 0); } void dec(map<int, int>& m, int k) { if(m[k] > 1) { m[k]--; } else { m.erase(k); } } int solveStep(State* state) { int zeros = 0; int t = 0; map<int, int> ones(*(state->ones)); map<int, int> twos(*(state->twos)); while((!ones.empty()) || (!twos.empty())) { int last1 = -1; int last2 = -1; pair<int, int> s1; pair<int, int> s2; if(!ones.empty()) { last1 = ones.rbegin()->first; s1 = countZeros(last1, t); } if(!twos.empty()) { last2 = twos.rbegin()->first; s2 = countZeros2(last2, 2*t); } if((last1 >= 0) && (last2 >= 0)) { if(s1.first >= s2.first) { last2 = -1; } else { last1 = -1; } } if(last1 >= 0) { dec(ones, last1); zeros += s1.first; t += s1.second; } else { dec(twos, last2); zeros += s2.first; t += s2.second; } } return zeros; } int solve(State* state) { int full = solveStep(state); if(state->countOnes() <= 1) { return full; } int k = state->ones->rbegin()->first; State* r = state->withoutOne(k); int withoutOne = k + solveStep(r); delete r; return max(full, withoutOne); } int solve(char* a, int n) { bool oneExists = false; for(int i=0;i<n;i++) { if(a[i] == '1') { oneExists = true; break; } } if(!oneExists) { return 0; } State* state = new State(a, n); int res = n - solve(state); delete state; return res; } int main(int argc, char** argv) { int t, n; char a[100005]; scanf("%d", &t); for(int i=0;i<t;i++) { scanf("%d", &n); while(true) { char c = getchar(); if((c >= '0') && (c <= '1')) { a[0] = c; break; } } for(int j=1;j<n;j++) { a[j] = getchar(); } int res = solve(a, n); printf("%d\n", res); } return 0; } |