#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <string>
using namespace std;
class cmp {
public:
bool operator()(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first) return a.second > b.second;
return a.first < b.first;
}
};
int main() {
int z; cin >> z; while (z--) {
int n; cin >> n;
int in_1 = 0;
string inp;
cin >> inp;
inp = "2" + inp + "2";
int _1 = 0;
priority_queue<int> Q2;
priority_queue<int> Q1;
pair<int, int> temp;
vector<pair<int, int>> jedynka;
for (int i = 1; i < n + 2; i++) {
if (inp[i] == '0') {
if (inp[i - 1] == '0') temp.first++;
else temp = { 1,(int)(inp[i - 1] == '1') };
}
else {
_1+=(inp[i]=='1');
if (inp[i - 1] == '0') { temp.second += (int)(inp[i] == '1'); if (temp.second == 2) Q2.push(temp.first); else Q1.push(temp.first); }
}
}
//cout << _1 << 'k';
int dc = 0;
while (!Q2.empty()) {
if (Q1.empty() || Q1.top()+dc < Q2.top()) {
int x = Q2.top(); Q2.pop();
//cout << "(" << x << ",2)";
_1 += min(x, 2 * dc + 1);
dc += (int)(x >= 2 * dc + 1) + (int)(x > 2 * dc + 1);
//cout << "[" << _1 << "/" << dc << "]";
}
else {
int x = Q1.top(); Q1.pop();
//cout << "(" << x << ",1)";
_1 += min(x, dc);
dc++; //cout << "[" << _1 << "/" << dc << "]";
}
}
while (!Q1.empty()) {
int x = Q1.top(); Q1.pop();
//cout << "(" << x << ",1)";
_1 += min(x, dc);
dc++; //cout << "[" << _1 << "/" << dc << "]";
}
cout << _1 << 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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> #include <string> using namespace std; class cmp { public: bool operator()(pair<int, int> a, pair<int, int> b) { if (a.first == b.first) return a.second > b.second; return a.first < b.first; } }; int main() { int z; cin >> z; while (z--) { int n; cin >> n; int in_1 = 0; string inp; cin >> inp; inp = "2" + inp + "2"; int _1 = 0; priority_queue<int> Q2; priority_queue<int> Q1; pair<int, int> temp; vector<pair<int, int>> jedynka; for (int i = 1; i < n + 2; i++) { if (inp[i] == '0') { if (inp[i - 1] == '0') temp.first++; else temp = { 1,(int)(inp[i - 1] == '1') }; } else { _1+=(inp[i]=='1'); if (inp[i - 1] == '0') { temp.second += (int)(inp[i] == '1'); if (temp.second == 2) Q2.push(temp.first); else Q1.push(temp.first); } } } //cout << _1 << 'k'; int dc = 0; while (!Q2.empty()) { if (Q1.empty() || Q1.top()+dc < Q2.top()) { int x = Q2.top(); Q2.pop(); //cout << "(" << x << ",2)"; _1 += min(x, 2 * dc + 1); dc += (int)(x >= 2 * dc + 1) + (int)(x > 2 * dc + 1); //cout << "[" << _1 << "/" << dc << "]"; } else { int x = Q1.top(); Q1.pop(); //cout << "(" << x << ",1)"; _1 += min(x, dc); dc++; //cout << "[" << _1 << "/" << dc << "]"; } } while (!Q1.empty()) { int x = Q1.top(); Q1.pop(); //cout << "(" << x << ",1)"; _1 += min(x, dc); dc++; //cout << "[" << _1 << "/" << dc << "]"; } cout << _1 << endl; } return 0; } |
English