#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } void st() { int n; string s; cin >> n >> s; int pocz = 0; while(pocz < n && s[pocz] == '0') pocz++; if (pocz == n){ cout << 0 << endl; return; } priority_queue<int> J, D; if (pocz > 0) J.push(pocz); int kon = n - 1; while(s[kon] == '0') kon--; if (kon < n - 1) J.push(n - 1 - kon); int ile = 0; kon++; FOR(i,pocz,kon){ if (s[i] == '1' && ile != 0){ D.push(ile); ile = 0; } if (s[i] == '0') ile++; } int ocal = 0; int step = 0; while(J.size() > 0 || D.size() > 0){ bool pickD = false; if (J.size() == 0) pickD = true; else if (D.size() == 0) pickD = false; else { //cout << D.top() - step - (int) J.size() - (int) D.size() << endl; if (D.top() - step - (int) J.size() > J.top()) pickD = true; } if (pickD){ int x = D.top(); //cout << "Front d = " << x << endl; D.pop(); if (x - step * 2 > 0){ //cout << "Saved 1 from D. Push " << x - step - 1 << " cause got " << x << endl; ocal++; step++; J.push(x - step); } } else { int x = J.top(); J.pop(); if (x - step > 0){ //cout << "Saved " << x - step << " from J" << endl; ocal += x - step; step++; } } } cout << n - ocal << endl; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; FOR(i,0,t){ if (t < 50){ cerr << "-------------------" << endl; } //cout << "Case #" << i + 1 <<": "; st(); } }
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 95 96 97 98 99 100 101 102 103 104 105 | #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<b;++i) #define FORD(i,a,b) for(int i=a;i>=b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } void st() { int n; string s; cin >> n >> s; int pocz = 0; while(pocz < n && s[pocz] == '0') pocz++; if (pocz == n){ cout << 0 << endl; return; } priority_queue<int> J, D; if (pocz > 0) J.push(pocz); int kon = n - 1; while(s[kon] == '0') kon--; if (kon < n - 1) J.push(n - 1 - kon); int ile = 0; kon++; FOR(i,pocz,kon){ if (s[i] == '1' && ile != 0){ D.push(ile); ile = 0; } if (s[i] == '0') ile++; } int ocal = 0; int step = 0; while(J.size() > 0 || D.size() > 0){ bool pickD = false; if (J.size() == 0) pickD = true; else if (D.size() == 0) pickD = false; else { //cout << D.top() - step - (int) J.size() - (int) D.size() << endl; if (D.top() - step - (int) J.size() > J.top()) pickD = true; } if (pickD){ int x = D.top(); //cout << "Front d = " << x << endl; D.pop(); if (x - step * 2 > 0){ //cout << "Saved 1 from D. Push " << x - step - 1 << " cause got " << x << endl; ocal++; step++; J.push(x - step); } } else { int x = J.top(); J.pop(); if (x - step > 0){ //cout << "Saved " << x - step << " from J" << endl; ocal += x - step; step++; } } } cout << n - ocal << endl; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; FOR(i,0,t){ if (t < 50){ cerr << "-------------------" << endl; } //cout << "Case #" << i + 1 <<": "; st(); } } |