#650 (Div. 3)C. Social Distance

題目描述

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.

Input

The first line contains a single integer t (1≤t≤104) — 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≤k≤n≤2⋅105) — 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⋅105.

Output

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.

Example

input
6
6 1
100010
6 2
000000
5 1
10101
3 1
001
2 2
00
1 1
0
output
1
2
0
1
1
1

Note

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.

題目大意

給定01字串,若相鄰的1之間0的個數大於等於k,是有效的社交距離。給定一個01字串,將其中的0變成1,還能保持有效的社交距離,求最多的變換次數。

題目分析

用一個pos[]數組表示座位的狀態,pos[i]=true表示第i個位子可以坐人。
將字符串s正向遍歷一遍。如果s[i]=1,那麼讓pos[i , i+k]=false。
將字符串s反向遍歷一遍。如果s[i]=1,那麼讓pos[i-k , i]=false。
最後剩下的就是能坐人的位置了,不過注意在在最後統計空位的時候,還是要注意間隔。

代碼如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <set> 
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=2e5+5;
bool pos[N];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		memset(pos,true,sizeof pos);	//初始化
		int n,k;
		string s;
		cin>>n>>k;
		cin>>s;
		for(int i=0;i<s.size();i++)	//正向遍歷
		{
			if(s[i]=='1')	//s[i]==1,則將i———i+k的位置的pos賦成false
			{
				pos[i]=false;
				for(int j=0;j<k&&i<s.size();j++)
				{
					i++;
					pos[i]=false;
				}
			}
		}
		for(int i=s.size()-1;i>=0;i--)//反向遍歷
		{
			if(s[i]=='1')	//s[i]==1,則將i-k———i的位置的pos賦成false
			{
				pos[i]=false;
				for(int j=0;j<k&&i>0;j++)
				{
					i--;
					pos[i]=false;
				}
			}
		}
		int ans=0;		//統計空位
		for(int i=0;i<s.size();i++)
		{
			if(!pos[i]) continue;
			ans++;				//當找到某個位置可以坐人時,往後跳k位再繼續統計
			for(int j=0;j<k&&i<s.size();j++)
			{
				i++;
				pos[i]=false;
			}
		}
		
		cout<<ans<<endl;	
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章