#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(pair<float, float> a, pair<float, float> b) {
return a.first/a.second >= b.first/b.second;
}
void empty(vector<pair<int, int>>& a, int& s) {
for (int i = a.size() - 1; i > -1; i--) {
if (a[i].second > 0)
s += a[i].second;
a[i].first -= a[i].second;
if (a[i].first <= 0 || a[i].second <= 0)
a.erase(a.begin() + i);
if (a[i].first == 1 && a[i].second == 2)
a[i].second = 1;
}
}
void solve() {
int n;
string miasto;
cin >> n;
cin >> miasto;
miasto = "2" + miasto + "2";
vector<pair<int, int>> gaps;
bool saving = false;
int sum = 0;
for (int i = 0; i < n + 2; i++) {
if (miasto[i] == '1')
sum++;
if (!saving) {
if (miasto[i] == '0') {
gaps.push_back(pair<int, int>(1, miasto[i - 1] == '1'));
saving = true;
}
} else {
if (miasto[i] == '0')
gaps.back().first++;
else if (miasto[i] == '1') {
gaps.back().second++;
saving = false;
} else if (miasto[i] == '2') {
saving = false;
}
}
if (gaps.size() > 0 && gaps.back().first == 1 && gaps.back().second == 2)
gaps.back().second = 1;
}
sort(gaps.begin(), gaps.end(), [] (auto a, auto b) {return compare(a, b);});
while (gaps.size() > 0) {
gaps[0].first--;
gaps[0].second--;
empty(gaps, sum);
sort(gaps.begin(), gaps.end(), [] (auto a, auto b) {return compare(a, b);});
}
cout << sum << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t, n;
string miasto;
cin >> t;
for (int i = 0; i < t; i++) {
int puste = 0;
bool grouping = false;
solve();
}
}
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; bool compare(pair<float, float> a, pair<float, float> b) { return a.first/a.second >= b.first/b.second; } void empty(vector<pair<int, int>>& a, int& s) { for (int i = a.size() - 1; i > -1; i--) { if (a[i].second > 0) s += a[i].second; a[i].first -= a[i].second; if (a[i].first <= 0 || a[i].second <= 0) a.erase(a.begin() + i); if (a[i].first == 1 && a[i].second == 2) a[i].second = 1; } } void solve() { int n; string miasto; cin >> n; cin >> miasto; miasto = "2" + miasto + "2"; vector<pair<int, int>> gaps; bool saving = false; int sum = 0; for (int i = 0; i < n + 2; i++) { if (miasto[i] == '1') sum++; if (!saving) { if (miasto[i] == '0') { gaps.push_back(pair<int, int>(1, miasto[i - 1] == '1')); saving = true; } } else { if (miasto[i] == '0') gaps.back().first++; else if (miasto[i] == '1') { gaps.back().second++; saving = false; } else if (miasto[i] == '2') { saving = false; } } if (gaps.size() > 0 && gaps.back().first == 1 && gaps.back().second == 2) gaps.back().second = 1; } sort(gaps.begin(), gaps.end(), [] (auto a, auto b) {return compare(a, b);}); while (gaps.size() > 0) { gaps[0].first--; gaps[0].second--; empty(gaps, sum); sort(gaps.begin(), gaps.end(), [] (auto a, auto b) {return compare(a, b);}); } cout << sum << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t, n; string miasto; cin >> t; for (int i = 0; i < t; i++) { int puste = 0; bool grouping = false; solve(); } } |
English