#include <iostream>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
using namespace std;
int uratuj ( const vector<int> &obszary, int ruch, int uratowane ){
for(size_t i=0; i<obszary.size(); i++){
if (obszary[i]-ruch*2>=3){
uratowane += obszary[i]-ruch*2-1;
ruch+=2;
}else if (obszary[i]-ruch*2>0){
ruch++;
uratowane++;
}else
break;
}
return uratowane;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, t;
cin>>t;
string s;
while(t-->0){
cin>>n;
cin>>s;
vector <int> obszary;
int obszar = 0;
for (auto c:s){
if (c=='1'){
if (obszar>0){
obszary.push_back(obszar);
obszar =0 ;
}
}else{
obszar++;
}
}
if (obszar>0)
obszary.push_back(obszar);
int skraj1=0;
int skraj2=0;
if (s.back()=='0'){
skraj1=obszary.back();
obszary.pop_back();
}
if (s[0]=='0' && !obszary.empty()){
skraj2=obszary[0];
obszary[0]=obszary.back();
obszary.pop_back();
}
sort(begin(obszary), end(obszary), greater<int>());
int uratowane = uratuj(obszary, 0,0);
int uratowane1 = uratuj(obszary,1,max(skraj1,skraj2));
int uratowane2 = uratuj(obszary,2, skraj1+skraj2-1);
uratowane = max(uratowane, max(uratowane1,uratowane2));
cout<<n-(uratowane)<<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 | #include <iostream> #include <cstdint> #include <vector> #include <algorithm> #include <iterator> #include <string> using namespace std; int uratuj ( const vector<int> &obszary, int ruch, int uratowane ){ for(size_t i=0; i<obszary.size(); i++){ if (obszary[i]-ruch*2>=3){ uratowane += obszary[i]-ruch*2-1; ruch+=2; }else if (obszary[i]-ruch*2>0){ ruch++; uratowane++; }else break; } return uratowane; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, t; cin>>t; string s; while(t-->0){ cin>>n; cin>>s; vector <int> obszary; int obszar = 0; for (auto c:s){ if (c=='1'){ if (obszar>0){ obszary.push_back(obszar); obszar =0 ; } }else{ obszar++; } } if (obszar>0) obszary.push_back(obszar); int skraj1=0; int skraj2=0; if (s.back()=='0'){ skraj1=obszary.back(); obszary.pop_back(); } if (s[0]=='0' && !obszary.empty()){ skraj2=obszary[0]; obszary[0]=obszary.back(); obszary.pop_back(); } sort(begin(obszary), end(obszary), greater<int>()); int uratowane = uratuj(obszary, 0,0); int uratowane1 = uratuj(obszary,1,max(skraj1,skraj2)); int uratowane2 = uratuj(obszary,2, skraj1+skraj2-1); uratowane = max(uratowane, max(uratowane1,uratowane2)); cout<<n-(uratowane)<<endl; } return 0; } |
English