题面
You have classes today, which are numbered from to .
The classes are described by a binary string of length . We call class important if and only if . For each important class, you must stay awake and listen to it.
You are very tired and wish to sleep through as many classes as possible. However, falling asleep takes time. If you listen to an important class , then you cannot fall asleep for the next classes, i.e., you must also stay awake in classes (or until the end of the day, if fewer than classes remain).
For classes that are not important, you may sleep through them unless the rule above forces you to stay awake.
Your task is to find out the maximum number of classes you can sleep through today.
A binary string is a string where each character is either or .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains two integers and ().
The second line of each test case contains the string of length ( or ).
Output
For each test case, output a single integer — the maximum number of classes you can sleep through today.
思路
重要课位置固定必须清醒,因此只需统计哪些课被强制清醒
每个 会使区间 全部清醒,区间并集大小记为
可以得到结论:答案为 ,其本质是一个贪心
代码
#include <bits/stdc++.h>
using namespace std;
int main() { int t; cin >> t; while (t --) { int n,k; cin >> n >> k; string s; cin >> s;
int b = -1; int a = 0; for (int i = 0; i < n; i ++) { if (s[i] == '1') { a ++; b = max(b, i + k); } else if (i <= b) { a ++; } }
cout << n - a << endl; } return 0;}