You are given a string ss consisting of characters 0 and/or 1.
You have to remove several (possibly zero) characters from the beginning of the string, and then several (possibly zero) characters from the end of the string. The string may become empty after the removals. The cost of the removal is the maximum of the following two values:
- the number of characters 0 left in the string;
- the number of characters 1 removed from the string.
What is the minimum cost of removal you can achieve?
Input
The first line contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.
Each test case consists of one line containing the string ss (1≤|s|≤2⋅1051≤|s|≤2⋅105), consisting of characters 0 and/or 1.
The total length of strings ss in all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, print one integer — the minimum cost of removal you can achieve.
Example
input
5 101110110 1001001001001 0000111111 00000 1111
output
1 3 0 0 0
思路:二分答案+双指针判断当前答案是否符合
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define int long long
const int N=1000010;
int a[N];
int s1[N];
int s0[N];
int n;
string s;
bool check(int x)
{
for(int i=1,j=0;i<=n;i++)
{
while(j<i&&s0[i]-s0[j]>x) j++;
if(s1[j]+s1[n]-s1[i]<=x) return true;
}
return false;
}
signed main()
{
int t;
cin>>t;
while(t--)
{
cin>>s;
s=" "+s;
n=s.size();
int l=0,r=n;
for(int i=1;i<=n;i++)
{
if(s[i]=='1')
{
s1[i]=s1[i-1]+1;
s0[i]=s0[i-1];
}
else
{
s0[i]=s0[i-1]+1;
s1[i]=s1[i-1];
}
}
while(l<r)
{
int mid=l+r>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
cout<<l<<endl;
}
}