#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define piii pair<pii,pii>
#define precise cout<<fixed<<setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int MAX=1e5+5;
const int inf=1e18+9;
char a[MAX];
void solve(){
int n;
cin>>n;
int zeros=0;
for (int i=1;i<=n;i++)cin>>a[i],zeros+=(a[i]=='0');
multiset<int>ones,twos;ones.clear();twos.clear();
for (int i=1;i<=n;i++){
int j=i;
if (a[i]!='0')continue;
while (j+1<=n && a[j+1]=='0'){
j++;
}
if (i==1 || j==n)ones.ins(j-i+1);
else twos.ins(j-i+1);
i=j;
}
int odj1=0,odj2=0,ans=0;
for (int tempo=1;tempo<=n;tempo++){
while (sz(twos) && *twos.begin()<=odj2)twos.erase(twos.find(*twos.begin()));
while (sz(twos) && *twos.begin()-odj2<=1){
int x=*twos.begin();
twos.erase(twos.find(x));
x-=odj2;
x+=odj1;
ones.ins(x);
}
while (sz(ones) && *ones.begin()<=odj1)ones.erase(ones.find(*ones.begin()));
int as=-inf;
int bs=-inf;
if (sz(twos))as=(*twos.rbegin()-odj2);
if (sz(ones))bs=(*ones.rbegin()-odj1)*2;
if (as>=bs && as!=-inf){
int x=*twos.rbegin();
twos.erase(twos.find(x));
x--;
if (x>0)
ones.ins(x-odj2+odj1);
}
else if (bs>as && bs!=-inf){
int x=*ones.rbegin();
ones.erase(ones.find(x));
}
ans+=sz(twos)*2+sz(ones);
odj1++;
odj2+=2;
}
cout<<ans+n-zeros<<"\n";
}
int32_t main(){
BOOST;
int t;
cin>>t;
for (int z=0;z<t;z++){
solve();
}
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 | #include <bits/stdc++.h> #define int long long #define mp make_pair #define pb push_back #define ld long double #define pii pair<int,int> #define sz(x) (int)x.size() #define piii pair<pii,pii> #define precise cout<<fixed<<setprecision(10) #define st first #define nd second #define ins insert #define vi vector<int> #define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; const int MAX=1e5+5; const int inf=1e18+9; char a[MAX]; void solve(){ int n; cin>>n; int zeros=0; for (int i=1;i<=n;i++)cin>>a[i],zeros+=(a[i]=='0'); multiset<int>ones,twos;ones.clear();twos.clear(); for (int i=1;i<=n;i++){ int j=i; if (a[i]!='0')continue; while (j+1<=n && a[j+1]=='0'){ j++; } if (i==1 || j==n)ones.ins(j-i+1); else twos.ins(j-i+1); i=j; } int odj1=0,odj2=0,ans=0; for (int tempo=1;tempo<=n;tempo++){ while (sz(twos) && *twos.begin()<=odj2)twos.erase(twos.find(*twos.begin())); while (sz(twos) && *twos.begin()-odj2<=1){ int x=*twos.begin(); twos.erase(twos.find(x)); x-=odj2; x+=odj1; ones.ins(x); } while (sz(ones) && *ones.begin()<=odj1)ones.erase(ones.find(*ones.begin())); int as=-inf; int bs=-inf; if (sz(twos))as=(*twos.rbegin()-odj2); if (sz(ones))bs=(*ones.rbegin()-odj1)*2; if (as>=bs && as!=-inf){ int x=*twos.rbegin(); twos.erase(twos.find(x)); x--; if (x>0) ones.ins(x-odj2+odj1); } else if (bs>as && bs!=-inf){ int x=*ones.rbegin(); ones.erase(ones.find(x)); } ans+=sz(twos)*2+sz(ones); odj1++; odj2+=2; } cout<<ans+n-zeros<<"\n"; } int32_t main(){ BOOST; int t; cin>>t; for (int z=0;z<t;z++){ solve(); } return 0; } |
English