【每日一题】CF1680C. Binary String | 双指针 | 简单

题目内容

原题链接

给定一个长度为 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 1n2×105

题解

解法1

二分答案 m i d mid mid,枚举子串右端点,当 x ≥ y x\geq y xy ,则不停移动左端点。然后取 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 xy ,就需要不停移动左端点,直到 x ≤ y x\leq y xy
这样就不需要二分答案了,只是一个双指针。

时间复杂度: 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值