#include <iostream> #include<vector> #include<string> #include<algorithm> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t,n; cin>>t; string s; while(t--){ cin>>n>>s; vector<int>mid,kon; int i = 0; while(i<s.size() && s[i]=='0'){++i;} if(i==s.size()){ cout<<0<<'\n'; continue; } kon.push_back(i); i=n-1; while(i>=0 && s[i]=='0'){--i;} kon.push_back(n-1-i); if(kon.size()==2 && kon[1]<kon[0])swap(kon[1],kon[0]); for(int i = 0;i<n;++i){ if(s[i]=='0'){ int j = i; while(j<n && s[j]=='0'){++j;} mid.push_back(j-i); i=j-1; } } if(s[0]=='0')mid.erase(mid.begin()); if(s[s.size()-1]=='0')mid.erase(mid.end()-1); sort(mid.begin(),mid.end()); int day = 0; int ile=0; int imid=mid.size()-1; int ikon=kon.size()-1; while(true){ int omid=-1, okon=-1; if(imid>=0)omid=mid[imid]-1-2*day; if(imid>=0 && omid==0 && mid[imid]==2*day+1)omid=1; if(ikon>=0)okon=kon[ikon]-day; //cout<<omid<<" "<<okon<<endl; if(okon<0 && omid<0)break; if(okon>=omid){ile+=okon;--ikon;++day;} else{ile+=omid;--imid;day+=2;} } int odp1=n-ile; day = 1; ile=kon[1]; imid=mid.size()-1; ikon=0; while(true){ int omid=-1, okon=-1; if(imid>=0)omid=mid[imid]-1-2*day; if(imid>=0 && omid==0 && mid[imid]==2*day+1)omid=1; if(ikon>=0)okon=kon[ikon]-day; //cout<<omid<<" "<<okon<<endl; if(okon<0 && omid<0)break; if(okon>=omid){ile+=okon;--ikon;++day;} else{ile+=omid;--imid;day+=2;} } int odp2=n-ile; day = 2; ile=kon[1]+kon[0]-1; imid=mid.size()-1; ikon=-1; while(true){ int omid=-1, okon=-1; if(imid>=0)omid=mid[imid]-1-2*day; if(imid>=0 && omid==0 && mid[imid]==2*day+1)omid=1; if(ikon>=0)okon=kon[ikon]-day; //cout<<omid<<" "<<okon<<endl; if(okon<0 && omid<0)break; if(okon>=omid){ile+=okon;--ikon;++day;} else{ile+=omid;--imid;day+=2;} } int odp3 = n-ile; cout<<min(odp1,min(odp2,odp3))<<'\n'; } }
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 | #include <iostream> #include<vector> #include<string> #include<algorithm> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t,n; cin>>t; string s; while(t--){ cin>>n>>s; vector<int>mid,kon; int i = 0; while(i<s.size() && s[i]=='0'){++i;} if(i==s.size()){ cout<<0<<'\n'; continue; } kon.push_back(i); i=n-1; while(i>=0 && s[i]=='0'){--i;} kon.push_back(n-1-i); if(kon.size()==2 && kon[1]<kon[0])swap(kon[1],kon[0]); for(int i = 0;i<n;++i){ if(s[i]=='0'){ int j = i; while(j<n && s[j]=='0'){++j;} mid.push_back(j-i); i=j-1; } } if(s[0]=='0')mid.erase(mid.begin()); if(s[s.size()-1]=='0')mid.erase(mid.end()-1); sort(mid.begin(),mid.end()); int day = 0; int ile=0; int imid=mid.size()-1; int ikon=kon.size()-1; while(true){ int omid=-1, okon=-1; if(imid>=0)omid=mid[imid]-1-2*day; if(imid>=0 && omid==0 && mid[imid]==2*day+1)omid=1; if(ikon>=0)okon=kon[ikon]-day; //cout<<omid<<" "<<okon<<endl; if(okon<0 && omid<0)break; if(okon>=omid){ile+=okon;--ikon;++day;} else{ile+=omid;--imid;day+=2;} } int odp1=n-ile; day = 1; ile=kon[1]; imid=mid.size()-1; ikon=0; while(true){ int omid=-1, okon=-1; if(imid>=0)omid=mid[imid]-1-2*day; if(imid>=0 && omid==0 && mid[imid]==2*day+1)omid=1; if(ikon>=0)okon=kon[ikon]-day; //cout<<omid<<" "<<okon<<endl; if(okon<0 && omid<0)break; if(okon>=omid){ile+=okon;--ikon;++day;} else{ile+=omid;--imid;day+=2;} } int odp2=n-ile; day = 2; ile=kon[1]+kon[0]-1; imid=mid.size()-1; ikon=-1; while(true){ int omid=-1, okon=-1; if(imid>=0)omid=mid[imid]-1-2*day; if(imid>=0 && omid==0 && mid[imid]==2*day+1)omid=1; if(ikon>=0)okon=kon[ikon]-day; //cout<<omid<<" "<<okon<<endl; if(okon<0 && omid<0)break; if(okon>=omid){ile+=okon;--ikon;++day;} else{ile+=omid;--imid;day+=2;} } int odp3 = n-ile; cout<<min(odp1,min(odp2,odp3))<<'\n'; } } |