#include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int z; cin >> z; for(int q = 1;q<=z;q++){ int n; cin >> n; vector<int> betwen_range; int left = 0,right = 0; string in; cin >> in; //if(q!=796) // continue; int obec = 0; bool ft = false; for(int k=0;k<in.size();k++) { if(in[k] == '0') obec++; else { if(obec != 0) { if(!ft) { left = obec; } else { betwen_range.push_back(obec); } } obec = 0; ft = true; } right = obec; } sort(betwen_range.begin(),betwen_range.end(),greater<int>()); /*cout << "LEFT = " << left << " " << "RIGHT = " << right<<endl; for(int y=0;y<10;y++) { cout << "MAM PRZEDZIAL" << betwen_range[y] <<endl; }*/ int t = 0; int res = 0; for(int k=0;k<betwen_range.size();k++) { if(left - t >= betwen_range[k] - 2*t -1 && left -t> 0) { //cout << "DODAJE PRZEDZIAL left = "<< left << " W CHWILI " << t <<endl; res+=left-t; left = 0; t++; k--; continue; } if(right -t >= betwen_range[k] - 2*t-1 && right -t > 0) { //cout << "DODAJE PRZEDZIAL right = "<< right << " W CHWILI " << t <<endl; res+=right-t; right = 0; t++; k--; continue; } if(left - t + right - t-1 > betwen_range[k] - 2*t-1 && right-t>0 && left-t>0 && left == right) { //cout << "DODAJE NA RAZ LEFT I RIGHT = " << left << " " << right << " " << "W CHWILI " << t <<endl; res+=right - t + left - t -1; left = right = 0; t+=2; k--; continue; } if(betwen_range[k] - 2*t > 0) { // cout << "DODAJE PRZEZIAL NONE = " << betwen_range[k] << " W CHWILI " << t<<endl; if(betwen_range[k] - 2*t == 1) { res+=1; t++; } else { res += betwen_range[k] - 2*t - 1; t+=2; } } } if(right -t > 0) { res+=right-t; //cout << "DODAJE PRZEDZIAL right = "<< right << " W CHWILI " << t <<endl; t++; } if( left -t > 0) { res+=left-t; //cout << "DODAJE PRZEDZIAL left = "<< left << " W CHWILI " << t <<endl; } cout << n - res <<endl; } }
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 | #include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int z; cin >> z; for(int q = 1;q<=z;q++){ int n; cin >> n; vector<int> betwen_range; int left = 0,right = 0; string in; cin >> in; //if(q!=796) // continue; int obec = 0; bool ft = false; for(int k=0;k<in.size();k++) { if(in[k] == '0') obec++; else { if(obec != 0) { if(!ft) { left = obec; } else { betwen_range.push_back(obec); } } obec = 0; ft = true; } right = obec; } sort(betwen_range.begin(),betwen_range.end(),greater<int>()); /*cout << "LEFT = " << left << " " << "RIGHT = " << right<<endl; for(int y=0;y<10;y++) { cout << "MAM PRZEDZIAL" << betwen_range[y] <<endl; }*/ int t = 0; int res = 0; for(int k=0;k<betwen_range.size();k++) { if(left - t >= betwen_range[k] - 2*t -1 && left -t> 0) { //cout << "DODAJE PRZEDZIAL left = "<< left << " W CHWILI " << t <<endl; res+=left-t; left = 0; t++; k--; continue; } if(right -t >= betwen_range[k] - 2*t-1 && right -t > 0) { //cout << "DODAJE PRZEDZIAL right = "<< right << " W CHWILI " << t <<endl; res+=right-t; right = 0; t++; k--; continue; } if(left - t + right - t-1 > betwen_range[k] - 2*t-1 && right-t>0 && left-t>0 && left == right) { //cout << "DODAJE NA RAZ LEFT I RIGHT = " << left << " " << right << " " << "W CHWILI " << t <<endl; res+=right - t + left - t -1; left = right = 0; t+=2; k--; continue; } if(betwen_range[k] - 2*t > 0) { // cout << "DODAJE PRZEZIAL NONE = " << betwen_range[k] << " W CHWILI " << t<<endl; if(betwen_range[k] - 2*t == 1) { res+=1; t++; } else { res += betwen_range[k] - 2*t - 1; t+=2; } } } if(right -t > 0) { res+=right-t; //cout << "DODAJE PRZEDZIAL right = "<< right << " W CHWILI " << t <<endl; t++; } if( left -t > 0) { res+=left-t; //cout << "DODAJE PRZEDZIAL left = "<< left << " W CHWILI " << t <<endl; } cout << n - res <<endl; } } |