#include <bits/stdc++.h> using namespace std; int t,n; string s; vector <pair <int, int> > values; void clr() { while(!values.empty()) { values.pop_back(); } } int main() { ios_base::sync_with_stdio(false); cin >> t; for(int i=0; i<t; i++) { cin >> n >> s; int curr = 0, res = 0, tura = 0; clr(); bool first = true; for(int j=0; j<n; j++) { if(s[j] == '0') curr++; else { if(first) { values.push_back(make_pair(2*curr, 1)); first = false; } else values.push_back(make_pair(curr, 2)); curr = 0; } } values.push_back(make_pair(2*curr, 1)); sort(values.begin(), values.end()); for(int j=values.size()-1; j>=0; j--) { int v = values[j].first; int t = values[j].second; //cout << v << ' ' << t << ' ' << tura << ' ' << res << endl; if(t==1) res+=max(v/2-t*tura,0); else res+=max(v-1-t*tura,0); if(t==2 && v-t*tura == 1) res++; ///case gdzie mamy pojedyncze pole zdrowe otoczone chorymi tura+=t; } cout << max(n-res,0) << 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 | #include <bits/stdc++.h> using namespace std; int t,n; string s; vector <pair <int, int> > values; void clr() { while(!values.empty()) { values.pop_back(); } } int main() { ios_base::sync_with_stdio(false); cin >> t; for(int i=0; i<t; i++) { cin >> n >> s; int curr = 0, res = 0, tura = 0; clr(); bool first = true; for(int j=0; j<n; j++) { if(s[j] == '0') curr++; else { if(first) { values.push_back(make_pair(2*curr, 1)); first = false; } else values.push_back(make_pair(curr, 2)); curr = 0; } } values.push_back(make_pair(2*curr, 1)); sort(values.begin(), values.end()); for(int j=values.size()-1; j>=0; j--) { int v = values[j].first; int t = values[j].second; //cout << v << ' ' << t << ' ' << tura << ' ' << res << endl; if(t==1) res+=max(v/2-t*tura,0); else res+=max(v-1-t*tura,0); if(t==2 && v-t*tura == 1) res++; ///case gdzie mamy pojedyncze pole zdrowe otoczone chorymi tura+=t; } cout << max(n-res,0) << endl; } } |