#include <bits/stdc++.h>
#define fi first
#define sc second
#define forn(i,p,k) for(int i=(p);i<=(k);++i)
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
vector<int> W;
int simulate(vector<int> P) {
int wyn = 0, mx;
while(!P.empty()) {
mx = 0;
for(const int &v:P) if(v > mx) mx = v;
if (mx <= 2) {
++wyn;
return wyn;
}
wyn += mx - 1;
W.clear();
for(const int &v:P) {
if(v != mx) {
if (v > 4) W.push_back(v-4);
} else mx = -1;
}
P.swap(W);
}
return wyn;
}
vector<int> V;
void MAIN()
{
int n,cnt = 0, first = -1, last = -1, total = 0;
V.clear();
W.clear();
cin>>n;
string Z;
cin>>Z;
forn(i,0,n-1) {
if (Z[i] == '0') {
cnt++;
} else {
total++;
if(first == -1) {
first = cnt;
} else {
if (cnt != 0) V.push_back(cnt);
}
cnt = 0;
}
}
last = cnt;
if (total == 0) return void(cout<<0<<"\n");
if (total == n) return void(cout<<n<<"\n");
if (first == 0 && last == 0) cout<<n - simulate(V)<<"\n";
else {
if(first == 0 || last == 0) {
int tmp1 = first + last;
for(const int &v:V) if(v > 2) W.push_back(v-2);
tmp1 += simulate(W);
int tmp2 = simulate(V);
cout<<n - max(tmp1, tmp2)<<"\n";
return;
} else {
int tmp1 = first + last - 1;
for(const int &v:V) if(v > 4) W.push_back(v-4);
tmp1 += simulate(W);
W.clear();
for(const int &v:V) if(v > 2) W.push_back(v-2);
int tmp2 = max(first, last);
tmp2 += simulate(W);
int tmp3 = simulate(V);
cout<<n - max(tmp1,max(tmp2,tmp3))<<"\n";
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int Z;cin>>Z;
while(Z--) MAIN();
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 | #include <bits/stdc++.h> #define fi first #define sc second #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int,int> pii; vector<int> W; int simulate(vector<int> P) { int wyn = 0, mx; while(!P.empty()) { mx = 0; for(const int &v:P) if(v > mx) mx = v; if (mx <= 2) { ++wyn; return wyn; } wyn += mx - 1; W.clear(); for(const int &v:P) { if(v != mx) { if (v > 4) W.push_back(v-4); } else mx = -1; } P.swap(W); } return wyn; } vector<int> V; void MAIN() { int n,cnt = 0, first = -1, last = -1, total = 0; V.clear(); W.clear(); cin>>n; string Z; cin>>Z; forn(i,0,n-1) { if (Z[i] == '0') { cnt++; } else { total++; if(first == -1) { first = cnt; } else { if (cnt != 0) V.push_back(cnt); } cnt = 0; } } last = cnt; if (total == 0) return void(cout<<0<<"\n"); if (total == n) return void(cout<<n<<"\n"); if (first == 0 && last == 0) cout<<n - simulate(V)<<"\n"; else { if(first == 0 || last == 0) { int tmp1 = first + last; for(const int &v:V) if(v > 2) W.push_back(v-2); tmp1 += simulate(W); int tmp2 = simulate(V); cout<<n - max(tmp1, tmp2)<<"\n"; return; } else { int tmp1 = first + last - 1; for(const int &v:V) if(v > 4) W.push_back(v-4); tmp1 += simulate(W); W.clear(); for(const int &v:V) if(v > 2) W.push_back(v-2); int tmp2 = max(first, last); tmp2 += simulate(W); int tmp3 = simulate(V); cout<<n - max(tmp1,max(tmp2,tmp3))<<"\n"; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int Z;cin>>Z; while(Z--) MAIN(); return 0; } |
English