#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
vector <int> edge;
while(t--) {
int n, res = 0;
int i = 0;
int count = 0;
int used = 0;
scanf("\n%d\n", &n);
while(i++ < n) {
char c = getchar();
if(c == '1')
break;
else
count++;
}
if(count > 1) {
res += count;
++used;
} else {
edge.push_back(-count);
}
count = 0;
while(i++ < n) {
char c = getchar();
if(c == '1' && count != 0){
edge.push_back(-count);
count = 0;
} else {
++count;
}
}
if(count - used > 1) {
res += count - used;
++used;
}
sort(edge.begin(), edge.end());
for(int j = 0; j < edge.size(); ++j) {
if(-edge[j] - used * 2 <= 0)
break;
else if(-edge[j] - used * 2 >= 2)
res += -edge[j] - used * 2 - 1;
else
++res;
used += 2;
}
if(count - used == 1)
res += count - used;
edge.clear();
printf("%d\n", 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 | #include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); vector <int> edge; while(t--) { int n, res = 0; int i = 0; int count = 0; int used = 0; scanf("\n%d\n", &n); while(i++ < n) { char c = getchar(); if(c == '1') break; else count++; } if(count > 1) { res += count; ++used; } else { edge.push_back(-count); } count = 0; while(i++ < n) { char c = getchar(); if(c == '1' && count != 0){ edge.push_back(-count); count = 0; } else { ++count; } } if(count - used > 1) { res += count - used; ++used; } sort(edge.begin(), edge.end()); for(int j = 0; j < edge.size(); ++j) { if(-edge[j] - used * 2 <= 0) break; else if(-edge[j] - used * 2 >= 2) res += -edge[j] - used * 2 - 1; else ++res; used += 2; } if(count - used == 1) res += count - used; edge.clear(); printf("%d\n", n-res); } return 0; } |
English