#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
int n;
vector<int> a;
int solve(){
vector<array<int, 3>> czasy;
int last = 1, wypelnione = 0;
for(int i = 1; i <= n; i++){
wypelnione += a[i];
if(a[i] == 1 || i == n){
int dlg = (i-1) - last + 1;
if(i == n && a[n] == 0)
dlg++;
if(dlg > 0){
if((last == 1 && a[1] == 0) || a[i] == 0)
czasy.push_back({dlg, -1, dlg});
else
czasy.push_back({(dlg / 2) + (dlg % 2), -2, dlg});
}
last = i + 1;
}
}
sort(czasy.rbegin(), czasy.rend());
// for(auto t : czasy)
// cout << "{ " << t[0] << " , " << t[1] << " , " << t[2] << " } ";
int nr_ruchu = 0;
for(auto t : czasy){
// cout << nr_ruchu << ": " << "{ " << t[0] << " , " << t[1] << " , " << t[2] << " } --> before: " << wypelnione << " after: ";
if(nr_ruchu >= t[0]){
wypelnione += t[2];
// cout << wypelnione << "\n";
continue;
}
else
wypelnione += -t[1] * nr_ruchu;
if(-t[1] == 2){
if(nr_ruchu + 1 == t[0]){
nr_ruchu++;
if(t[2] % 2 == 0)
wypelnione++;
}
else
nr_ruchu += 2, wypelnione++;
} else{
nr_ruchu++;
}
// cout << wypelnione << "\n";
}
return wypelnione;
}
int main(){
fastio;
int t;
cin >> t;
while(t--){
string s;
cin >> n >> s;
a.assign(n+1, 0);
for(int i = 1; i <= n; i++)
a[i] = (s[i-1] == '1') ? 1 : 0;
cout << solve() << "\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 | #include <bits/stdc++.h> using namespace std; #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) int n; vector<int> a; int solve(){ vector<array<int, 3>> czasy; int last = 1, wypelnione = 0; for(int i = 1; i <= n; i++){ wypelnione += a[i]; if(a[i] == 1 || i == n){ int dlg = (i-1) - last + 1; if(i == n && a[n] == 0) dlg++; if(dlg > 0){ if((last == 1 && a[1] == 0) || a[i] == 0) czasy.push_back({dlg, -1, dlg}); else czasy.push_back({(dlg / 2) + (dlg % 2), -2, dlg}); } last = i + 1; } } sort(czasy.rbegin(), czasy.rend()); // for(auto t : czasy) // cout << "{ " << t[0] << " , " << t[1] << " , " << t[2] << " } "; int nr_ruchu = 0; for(auto t : czasy){ // cout << nr_ruchu << ": " << "{ " << t[0] << " , " << t[1] << " , " << t[2] << " } --> before: " << wypelnione << " after: "; if(nr_ruchu >= t[0]){ wypelnione += t[2]; // cout << wypelnione << "\n"; continue; } else wypelnione += -t[1] * nr_ruchu; if(-t[1] == 2){ if(nr_ruchu + 1 == t[0]){ nr_ruchu++; if(t[2] % 2 == 0) wypelnione++; } else nr_ruchu += 2, wypelnione++; } else{ nr_ruchu++; } // cout << wypelnione << "\n"; } return wypelnione; } int main(){ fastio; int t; cin >> t; while(t--){ string s; cin >> n >> s; a.assign(n+1, 0); for(int i = 1; i <= n; i++) a[i] = (s[i-1] == '1') ? 1 : 0; cout << solve() << "\n"; } } |
English