#include <bits/stdc++.h> using namespace std; int w,n,k,b,poc,kon,s,it,p,ile,tab[100001]; string a; bool c1,c2; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n; for(int i=0;i<n;++i) { cin>>k; cin>>a; poc=0;kon=k-1;w=0;ile=0;p=0;it=0; c1=true;c2=true; for(int i=0;i<k;++i){if(a[i]=='0')ile++;} while(c1&&poc<k) { if(a[poc]=='0') { poc++; } else{c1=false;} } while(c2&&kon>0) { if(a[kon]=='0') { kon--; } else{c2=false;} } kon++; //cout<<poc<<" "<<kon; for(int i=poc+1;i<kon;++i) { if(a[i]=='0') { s++; } else if(a[i]=='1') { tab[it]=s; it++; s=0; } } //it--; sort(tab,tab+it);kon=k-kon; //for(int i=0;i<it;++i){cout<<tab[i]<<" ";}cout<<"\n"; //cout<<it<<" "<<poc<<" "<<kon<<"\n"; it--; if(ile==k){cout<<0<<"\n";} else { while(tab[it]-2*p>0) { // cout<<tab[it]-2*p-1<<" "<<kon-p<<" "<<poc-p<<"\n"; if(poc-p>=tab[it]-2*p-1) { w+=(poc-p); poc=0; p++; } else if(tab[it]-2*p-1<2) { p++; it--; w++; } else if(kon-p>=tab[it]-2*p-1) { w+=(kon-p); kon=0; p++; } else { w+=tab[it]-2*p-1; it--; p+=2; } } if(kon-p>0){w+=kon-p;p++;} if(poc-p>0){w+=poc-p;p++;} //cout<<w; cout<<k-w<<"\n"; } for(int i=0;i<k;++i){tab[i]=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 | #include <bits/stdc++.h> using namespace std; int w,n,k,b,poc,kon,s,it,p,ile,tab[100001]; string a; bool c1,c2; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n; for(int i=0;i<n;++i) { cin>>k; cin>>a; poc=0;kon=k-1;w=0;ile=0;p=0;it=0; c1=true;c2=true; for(int i=0;i<k;++i){if(a[i]=='0')ile++;} while(c1&&poc<k) { if(a[poc]=='0') { poc++; } else{c1=false;} } while(c2&&kon>0) { if(a[kon]=='0') { kon--; } else{c2=false;} } kon++; //cout<<poc<<" "<<kon; for(int i=poc+1;i<kon;++i) { if(a[i]=='0') { s++; } else if(a[i]=='1') { tab[it]=s; it++; s=0; } } //it--; sort(tab,tab+it);kon=k-kon; //for(int i=0;i<it;++i){cout<<tab[i]<<" ";}cout<<"\n"; //cout<<it<<" "<<poc<<" "<<kon<<"\n"; it--; if(ile==k){cout<<0<<"\n";} else { while(tab[it]-2*p>0) { // cout<<tab[it]-2*p-1<<" "<<kon-p<<" "<<poc-p<<"\n"; if(poc-p>=tab[it]-2*p-1) { w+=(poc-p); poc=0; p++; } else if(tab[it]-2*p-1<2) { p++; it--; w++; } else if(kon-p>=tab[it]-2*p-1) { w+=(kon-p); kon=0; p++; } else { w+=tab[it]-2*p-1; it--; p+=2; } } if(kon-p>0){w+=kon-p;p++;} if(poc-p>0){w+=poc-p;p++;} //cout<<w; cout<<k-w<<"\n"; } for(int i=0;i<k;++i){tab[i]=0;} } } |