题目内容
给定一个长度为 n n n 的 01 01 01 字符串,对于一个子串 s u b sub sub ,子串内部的 0 0 0 的数量为 x x x ,子串以外的 1 1 1 的数量为 y y y ,子串的代价为 m a x ( x , y ) max(x, y) max(x,y) ,问代价最小是多少。
数据范围
- 1 ≤ n ≤ 2 × 1 0 5 1\leq n \leq 2\times 10^5 1≤n≤2×105
题解
解法1
二分答案 m i d mid mid,枚举子串右端点,当 x ≥ y x\geq y x≥y ,则不停移动左端点。然后取 m a x max max 判断是否存在一个子串的代价小于等于 m i d mid mid 。
时间复杂度: O ( n log n ) O(n\log n) O(nlogn)
解法2
从二分答案中可以考虑到,枚举右端点,当
x
≥
y
x\geq y
x≥y ,就需要不停移动左端点,直到
x
≤
y
x\leq y
x≤y 。
这样就不需要二分答案了,只是一个双指针。
时间复杂度: O ( n ) O(n) O(n)
代码
#include <bits/stdc++.h>
using namespace std;
void solve() {
string s;
cin >> s;
int n = int(s.size());
int all1 = 0;
for (auto c: s) all1 += c == '1';
int ans = n - all1;
int in0 = 0, out1 = all1;
for (int r = 0, l = 0; r < n; ++r) {
int v = s[r] - '0';
if (v == 0) in0 += 1;
else out1 -= 1;
while (l <= r && in0 > out1) {
v = s[l] - '0';
if (v == 0) in0 -= 1;
else out1 += 1;
l += 1;
}
ans = min(ans, max(in0, out1));
}
cout << ans << "\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) solve();
return 0;
}