#include <cstdio>
#include <vector>
using namespace std;
struct safety {
int length;
int unvaxxedEnds;
};
void main1() {
int n;
scanf("\n%d\n", &n);
vector<safety> v;
char c, cc = '1';
for(int i=0; i<n; i++) {
scanf("%c", &c);
if(c == '0') {
if (cc == '0') {
v.back().length++;
} else {
v.push_back({ 1, 2 });
}
if (i == 0) {
v.back().unvaxxedEnds--;
}
if (i + 1 == n) {
v.back().unvaxxedEnds--;
}
}
cc = c;
}
bool hasSpread = !v.empty();
while(hasSpread) {
hasSpread = false;
int max = -1, maxIndex = -1;
for(int i=0; i<v.size(); i++) {
if ((max < v[i].length || (max == v[i].length && v[i].unvaxxedEnds < 2)) && v[i].unvaxxedEnds > 0) {
max = v[i].length;
maxIndex = i;
}
}
if (maxIndex >= 0) {
v[maxIndex].unvaxxedEnds--;
for(int i=0; i<v.size(); i++) {
if (v[i].unvaxxedEnds > 0 && v[i].length > 0) {
hasSpread = true;
v[i].length -= v[i].unvaxxedEnds;
if (v[i].length < 0) v[i].length = 0;
}
}
}
}
for(int i=0; i<v.size(); i++) {
n -= v[i].length;
}
printf("%d\n", n);
}
int main() {
int t;
scanf("%d", &t);
while(t--) main1();
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 | #include <cstdio> #include <vector> using namespace std; struct safety { int length; int unvaxxedEnds; }; void main1() { int n; scanf("\n%d\n", &n); vector<safety> v; char c, cc = '1'; for(int i=0; i<n; i++) { scanf("%c", &c); if(c == '0') { if (cc == '0') { v.back().length++; } else { v.push_back({ 1, 2 }); } if (i == 0) { v.back().unvaxxedEnds--; } if (i + 1 == n) { v.back().unvaxxedEnds--; } } cc = c; } bool hasSpread = !v.empty(); while(hasSpread) { hasSpread = false; int max = -1, maxIndex = -1; for(int i=0; i<v.size(); i++) { if ((max < v[i].length || (max == v[i].length && v[i].unvaxxedEnds < 2)) && v[i].unvaxxedEnds > 0) { max = v[i].length; maxIndex = i; } } if (maxIndex >= 0) { v[maxIndex].unvaxxedEnds--; for(int i=0; i<v.size(); i++) { if (v[i].unvaxxedEnds > 0 && v[i].length > 0) { hasSpread = true; v[i].length -= v[i].unvaxxedEnds; if (v[i].length < 0) v[i].length = 0; } } } } for(int i=0; i<v.size(); i++) { n -= v[i].length; } printf("%d\n", n); } int main() { int t; scanf("%d", &t); while(t--) main1(); return 0; } |
English