#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define mid (l+r)/2
#define PI acos((ld)-1)
#define BOOST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<vi> vvi;
void solve(){
int n;
string s;
cin >> n >> s;
int zero = 0, one = 0;
bool be = 0;
vector<int> t;
for(int i=0; i<n; i++){
if(s[i] == '0'){
zero++;
if(i == n-1){
if(be)
t.pb(zero);
continue;
}
if(s[i+1] == '1'){
if(be)
t.pb(zero/2), t.pb((zero+1)/2);
else
t.pb(zero);
}
}
if(s[i] == '1'){
one++;
be = 1;
zero = 0;
}
}
sort(all(t), greater<int>());
for(int i=0; i<t.size(); i++){
one += min(t[i], i);
}
cout << one << "\n";
}
int main(){
BOOST;
int q;
cin >> q;
while(q--)
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 | #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define mid (l+r)/2 #define PI acos((ld)-1) #define BOOST ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) typedef long long ll; typedef long double ld; typedef pair<int, int> ii; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<ii> vii; typedef vector<vi> vvi; void solve(){ int n; string s; cin >> n >> s; int zero = 0, one = 0; bool be = 0; vector<int> t; for(int i=0; i<n; i++){ if(s[i] == '0'){ zero++; if(i == n-1){ if(be) t.pb(zero); continue; } if(s[i+1] == '1'){ if(be) t.pb(zero/2), t.pb((zero+1)/2); else t.pb(zero); } } if(s[i] == '1'){ one++; be = 1; zero = 0; } } sort(all(t), greater<int>()); for(int i=0; i<t.size(); i++){ one += min(t[i], i); } cout << one << "\n"; } int main(){ BOOST; int q; cin >> q; while(q--) solve(); } |
English