A13501 | Social Distance
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Polycarp and his friends want to visit a new restaurant. The restaurant has $n$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $1$ to $n$ in the order from left to right. The state of the restaurant is described by a string of length $n$ which contains characters "1" (the table is occupied) and "0" (the table is empty).
Restaurant rules prohibit people to sit at a distance of $k$ or less from each other. That is, if a person sits at the table number $i$ , then all tables with numbers from $i-k$ to $i+k$ (except for the $i$ -th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $k$ .
For example, if $n=8$ and $k=2$ , then:
- strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant;
- strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $k=2$ .
In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.
You are given a binary string $s$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $s$ .
Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?
For example, if $n=6$ , $k=1$ , $s=$ "100010", then the answer to the problem will be $1$ , since only the table at position $3$ can be occupied such that the rules are still satisfied.
Restaurant rules prohibit people to sit at a distance of $k$ or less from each other. That is, if a person sits at the table number $i$ , then all tables with numbers from $i-k$ to $i+k$ (except for the $i$ -th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $k$ .
For example, if $n=8$ and $k=2$ , then:
- strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant;
- strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $k=2$ .
In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied.
You are given a binary string $s$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $s$ .
Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied?
For example, if $n=6$ , $k=1$ , $s=$ "100010", then the answer to the problem will be $1$ , since only the table at position $3$ can be occupied such that the rules are still satisfied.
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of test cases in the test. Then $t$ test cases follow.
Each test case starts with a line containing two integers $n$ and $k$ ( $1 \le k \le n \le 2\cdot 10^5$ ) — the number of tables in the restaurant and the minimum allowed distance between two people.
The second line of each test case contains a binary string $s$ of length $n$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $k$ .
The sum of $n$ for all test cases in one test does not exceed $2\cdot 10^5$ .
Each test case starts with a line containing two integers $n$ and $k$ ( $1 \le k \le n \le 2\cdot 10^5$ ) — the number of tables in the restaurant and the minimum allowed distance between two people.
The second line of each test case contains a binary string $s$ of length $n$ consisting of "0" and "1" — a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurant — the difference between indices of any two "1" is more than $k$ .
The sum of $n$ for all test cases in one test does not exceed $2\cdot 10^5$ .
输出格式
For each test case output one integer — the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $0$ .
输入输出样例
输入 #1
6 6 1 100010 6 2 000000 5 1 10101 3 1 001 2 2 00 1 1 0
输出 #1
1 2 0 1 1 1
The first test case is explained in the statement.
In the second test case, the answer is $2$ , since you can choose the first and the sixth table.
In the third test case, you cannot take any free table without violating the rules of the restaurant.
In the second test case, the answer is $2$ , since you can choose the first and the sixth table.
In the third test case, you cannot take any free table without violating the rules of the restaurant.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted