//
#include <bits/stdc++.h>
using namespace std;
#define FOR(n, u) for(int (n) = 1; (n) <= (u); n++)
const int max_n = 500002;
bool tab[max_n];
list <int> lista;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, n;
cin >> t;
char x;
FOR(apom, t){
cin >> n;
FOR(j, n){
cin >> x;
if(x == '0')
tab[j] = 0;
else
tab[j] = 1;
}
int i = 1;
while(i <= n){
if(tab[i] == 0){
int il = 0;
while(i <= n && tab[i] == 0){
// cout << tab[i] << " " << i << endl;
i++;
il++;
}
lista.push_back(il);
// cout << il << " ";
}
i++;
}
//cout << endl;
auto wskp = lista.begin();
auto wskk = lista.end();
if(tab[1] == 0)
wskp++;
if(tab[n] == 0)
wskk--;
for(auto a = wskp; a != wskk; a++){
if(*a == 1){
continue;
}
else if(*a % 2 == 0){
lista.push_front(*a / 2);
*a /= 2;
}
else{
lista.push_front(*a / 2 + 1);
*a /= 2;
}
}
lista.sort();
int il = 0;
long long suma = 0;
for(auto wsk = lista.rbegin(); wsk != lista.rend(); wsk++){
if(*wsk - il <= 0)
break;
suma += *wsk - il;
il++;
}
cout << n - suma << '\n';
lista.clear();
}
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 | // #include <bits/stdc++.h> using namespace std; #define FOR(n, u) for(int (n) = 1; (n) <= (u); n++) const int max_n = 500002; bool tab[max_n]; list <int> lista; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t, n; cin >> t; char x; FOR(apom, t){ cin >> n; FOR(j, n){ cin >> x; if(x == '0') tab[j] = 0; else tab[j] = 1; } int i = 1; while(i <= n){ if(tab[i] == 0){ int il = 0; while(i <= n && tab[i] == 0){ // cout << tab[i] << " " << i << endl; i++; il++; } lista.push_back(il); // cout << il << " "; } i++; } //cout << endl; auto wskp = lista.begin(); auto wskk = lista.end(); if(tab[1] == 0) wskp++; if(tab[n] == 0) wskk--; for(auto a = wskp; a != wskk; a++){ if(*a == 1){ continue; } else if(*a % 2 == 0){ lista.push_front(*a / 2); *a /= 2; } else{ lista.push_front(*a / 2 + 1); *a /= 2; } } lista.sort(); int il = 0; long long suma = 0; for(auto wsk = lista.rbegin(); wsk != lista.rend(); wsk++){ if(*wsk - il <= 0) break; suma += *wsk - il; il++; } cout << n - suma << '\n'; lista.clear(); } return 0; } |
English