#include<bits/stdc++.h> using namespace std; const int N = 1e5+3; int n,L=0,R=0; int T[N+3],T2[N+3]; string inp; vector<int> v; void zeruj(){ v.clear(); L = 0; R = 0; inp = ""; for(int i = 1;i<=n;i++){ T2[i]=0; T[i]=0; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int zap; cin>>zap; while(zap--){ zeruj(); cin>>n; cin>>inp; bool flag = true; for(int i = 1;i<=n;i++){ T[i] = (inp[i-1]=='1'); if(T[i]){ flag = false; } } if(flag){ cout<<0<<endl; continue; } while(T[L+1]!=1){ L += 1; } R = n+1; while(T[R-1]!=1){ R -= 1; } int ost = R; ost -= 1; R = n-R + 1; int p = L+1; for(int k = p;k<=ost;k++){ if(T[k]==1){ if(T[p]==0) v.push_back(k-p); p = k+1; } } sort(v.begin(),v.end()); reverse(v.begin(),v.end()); int n2 = v.size(); if(n2==0){ cout<<n - (L+R-(L !=0 && R != 0))<<endl; continue; } int suma1 = 0; for(int i = 0;i<v.size();i++){ T2[i+1] = v[i]; } for(int i = 1;i<=n2;i++){ int d = T2[i] - (i-1)*4; if(d <= 0){ break; } if(d == 1 || d == 2){ suma1 += 1; }else{ suma1 += d-1; } } if(L < R){ swap(L,R); } int suma = L; for(int i = 1;i<=n2;i++){ int x = T2[i] - 2 - 4*(i-1); if(x <= 0){ break; } if(x == 1 || x == 2){ suma += 1; continue; } suma += x-1; } int suma2 = L+R-1; for(int i = 1;i<=n2;i++){ int x = T2[i] - 4 - 4*(i-1); if(x <= 0){ break; } if(x == 1 || x == 2){ suma2 += 1; continue; } suma2 += x-1; } int best = max({suma,suma2,suma1}); cout<<n-best<<endl; } 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 | #include<bits/stdc++.h> using namespace std; const int N = 1e5+3; int n,L=0,R=0; int T[N+3],T2[N+3]; string inp; vector<int> v; void zeruj(){ v.clear(); L = 0; R = 0; inp = ""; for(int i = 1;i<=n;i++){ T2[i]=0; T[i]=0; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int zap; cin>>zap; while(zap--){ zeruj(); cin>>n; cin>>inp; bool flag = true; for(int i = 1;i<=n;i++){ T[i] = (inp[i-1]=='1'); if(T[i]){ flag = false; } } if(flag){ cout<<0<<endl; continue; } while(T[L+1]!=1){ L += 1; } R = n+1; while(T[R-1]!=1){ R -= 1; } int ost = R; ost -= 1; R = n-R + 1; int p = L+1; for(int k = p;k<=ost;k++){ if(T[k]==1){ if(T[p]==0) v.push_back(k-p); p = k+1; } } sort(v.begin(),v.end()); reverse(v.begin(),v.end()); int n2 = v.size(); if(n2==0){ cout<<n - (L+R-(L !=0 && R != 0))<<endl; continue; } int suma1 = 0; for(int i = 0;i<v.size();i++){ T2[i+1] = v[i]; } for(int i = 1;i<=n2;i++){ int d = T2[i] - (i-1)*4; if(d <= 0){ break; } if(d == 1 || d == 2){ suma1 += 1; }else{ suma1 += d-1; } } if(L < R){ swap(L,R); } int suma = L; for(int i = 1;i<=n2;i++){ int x = T2[i] - 2 - 4*(i-1); if(x <= 0){ break; } if(x == 1 || x == 2){ suma += 1; continue; } suma += x-1; } int suma2 = L+R-1; for(int i = 1;i<=n2;i++){ int x = T2[i] - 4 - 4*(i-1); if(x <= 0){ break; } if(x == 1 || x == 2){ suma2 += 1; continue; } suma2 += x-1; } int best = max({suma,suma2,suma1}); cout<<n-best<<endl; } return 0; } |