1 solutions

  • 0
    @ 2025-5-16 13:17:48

    本题考察经典问题 尺取法。

    我们最多把kk个1变成0.

    本质上我们要找出最长的一个子段,内部11的个数不能超过kk

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    const int N = 1e6 + 10;
    const int mod = 1e9 + 7;
    char s[N]; 
    signed main() {
    	ios::sync_with_stdio(false);
    	cin.tie(0); cout.tie(0);
        int n, k;
        cin >> n >> k;
        cin >> s + 1;
        int ans = 0;
        int j = 0, cur = 0; 
        for (int i = 1; i <= n; ++i) {
        	if (j < i - 1) {
        		j = i - 1;
        		cur = 0;
    		}
        	while (j + 1 <= n && cur + (s[j + 1] == '1') <= k) {
        		j ++;
        		cur += (s[j] == '1');
    		}
    		if (cur <= k)
    			ans = max(ans, j - i + 1);
    			
    		if (s[i] == '1') cur --;
    	}
    	cout << ans << endl;
        return 0;
    }
    
    • 1

    Information

    ID
    661
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    80
    Accepted
    3
    Uploaded By