#include "bits/stdc++.h" // Tomasz Nowak
using namespace std; // University of Warsaw
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
#define ssize(x) int(x.size())
template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) {
return o << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) {
o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}';
}
#ifdef DEBUG
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define debug(...) {}
#endif
int main() {
cin.tie(0)->sync_with_stdio(0);
int t;
cin >> t;
while(t --> 0) {
int n;
cin >> n;
string s;
cin >> s;
assert(ssize(s) == n);
if(*max_element(s.begin(), s.end()) == '0') {
cout << "0\n";
continue;
}
vector<pair<int, int>> gaps;
for(int l = -1; l < n - 1; ++l)
if((l == -1 or s[l] == '1') and s[l + 1] == '0') {
int r = l + 1;
while(r < n and s[r] == '0')
++r;
int gi = ssize(gaps);
if(l != -1 and r != n) {
gaps.emplace_back((r - l) / 2, gi + 1);
gaps.emplace_back((r - l - 1) / 2, gi);
}
else
gaps.emplace_back(l == -1 ? r : n - l - 1, -1);
}
debug(gaps);
set<pair<int, int>> rem_gaps;
REP(i, ssize(gaps))
if(gaps[i].second == -1 or i < gaps[i].second) {
int prio = 4 * gaps[i].first + 1;
if(gaps[i].second != -1)
prio = 2 * (gaps[i].first + gaps[gaps[i].second].first);
rem_gaps.emplace(prio, i);
}
debug(rem_gaps);
int answer = 0;
for(char c : s)
answer += bool(c == '1');
debug(answer);
for(int placed = 0; ssize(rem_gaps); ++placed) {
int i = -1;
while(not rem_gaps.empty()) {
int g_len;
tie(g_len, i) = *prev(rem_gaps.end());
debug("considering", g_len, i);
rem_gaps.erase(prev(rem_gaps.end()));
if(gaps[i].first > placed)
break;
debug("adding", i);
answer += gaps[i].first + (gaps[i].second == -1 ? 0 : gaps[gaps[i].second].first);
}
if(i == -1 or gaps[i].first <= placed)
continue;
answer += placed;
int j = gaps[i].second;
if(j != -1) {
if(gaps[i].first == placed + 1)
answer += gaps[j].first;
else
answer += ++placed;
}
}
cout << answer << '\n';
}
}
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 | #include "bits/stdc++.h" // Tomasz Nowak using namespace std; // University of Warsaw using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define debug(...) {} #endif int main() { cin.tie(0)->sync_with_stdio(0); int t; cin >> t; while(t --> 0) { int n; cin >> n; string s; cin >> s; assert(ssize(s) == n); if(*max_element(s.begin(), s.end()) == '0') { cout << "0\n"; continue; } vector<pair<int, int>> gaps; for(int l = -1; l < n - 1; ++l) if((l == -1 or s[l] == '1') and s[l + 1] == '0') { int r = l + 1; while(r < n and s[r] == '0') ++r; int gi = ssize(gaps); if(l != -1 and r != n) { gaps.emplace_back((r - l) / 2, gi + 1); gaps.emplace_back((r - l - 1) / 2, gi); } else gaps.emplace_back(l == -1 ? r : n - l - 1, -1); } debug(gaps); set<pair<int, int>> rem_gaps; REP(i, ssize(gaps)) if(gaps[i].second == -1 or i < gaps[i].second) { int prio = 4 * gaps[i].first + 1; if(gaps[i].second != -1) prio = 2 * (gaps[i].first + gaps[gaps[i].second].first); rem_gaps.emplace(prio, i); } debug(rem_gaps); int answer = 0; for(char c : s) answer += bool(c == '1'); debug(answer); for(int placed = 0; ssize(rem_gaps); ++placed) { int i = -1; while(not rem_gaps.empty()) { int g_len; tie(g_len, i) = *prev(rem_gaps.end()); debug("considering", g_len, i); rem_gaps.erase(prev(rem_gaps.end())); if(gaps[i].first > placed) break; debug("adding", i); answer += gaps[i].first + (gaps[i].second == -1 ? 0 : gaps[gaps[i].second].first); } if(i == -1 or gaps[i].first <= placed) continue; answer += placed; int j = gaps[i].second; if(j != -1) { if(gaps[i].first == placed + 1) answer += gaps[j].first; else answer += ++placed; } } cout << answer << '\n'; } } |
English