#include <bits/stdc++.h> using namespace std; // #define int long long void split(priority_queue<int>& q1, priority_queue<int>& q2, const string& s) { int ix = 0; while(ix < s.size()) { int end = ix; while(end < s.size() && s[end] == '0') ++end; if(ix != end) { int len = end-ix; if((ix == 0 && s[0] == '0') || end==s.size()) { // cout << "q1: " << len << endl; q1.push(len); } else { // cout << "q2: " << len << endl; q2.push(len); } } ix = end + 1; } } bool justZeros(const string& s) { for(char c : s) if(c == '1') return false; cout << 0 << endl; return true; } void solve() { int n; cin>>n; string s; cin >> s; if(justZeros(s)) return; priority_queue<int> q1,q2; split(q1, q2, s); int days = 0; int result = 0; while(q1.size() > 0 || q2.size() > 0) { int q1_val = -1e9; int q2_val = -1e9; if(!q1.empty()) q1_val = q1.top() - days; if(!q2.empty()) q2_val = q2.top() - days*2; if(q1_val*2 > q2_val) { q1.pop(); if(q1_val >0) result += q1_val; } else { q2.pop(); if(q2_val > 0) { q1.push(q2_val-1+days); result += 1; } } ++days; } // cout << "RESULT: " << result << endl; cout << s.size() - result << endl; return; } int main() { int z; cin >> z; while(z--) { solve(); } 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #include <bits/stdc++.h> using namespace std; // #define int long long void split(priority_queue<int>& q1, priority_queue<int>& q2, const string& s) { int ix = 0; while(ix < s.size()) { int end = ix; while(end < s.size() && s[end] == '0') ++end; if(ix != end) { int len = end-ix; if((ix == 0 && s[0] == '0') || end==s.size()) { // cout << "q1: " << len << endl; q1.push(len); } else { // cout << "q2: " << len << endl; q2.push(len); } } ix = end + 1; } } bool justZeros(const string& s) { for(char c : s) if(c == '1') return false; cout << 0 << endl; return true; } void solve() { int n; cin>>n; string s; cin >> s; if(justZeros(s)) return; priority_queue<int> q1,q2; split(q1, q2, s); int days = 0; int result = 0; while(q1.size() > 0 || q2.size() > 0) { int q1_val = -1e9; int q2_val = -1e9; if(!q1.empty()) q1_val = q1.top() - days; if(!q2.empty()) q2_val = q2.top() - days*2; if(q1_val*2 > q2_val) { q1.pop(); if(q1_val >0) result += q1_val; } else { q2.pop(); if(q2_val > 0) { q1.push(q2_val-1+days); result += 1; } } ++days; } // cout << "RESULT: " << result << endl; cout << s.size() - result << endl; return; } int main() { int z; cin >> z; while(z--) { solve(); } return 0; } |