当前位置:网站首页>C. Social Distance
C. Social Distance
2022-08-10 20:44:00 【秦小咩】
C. Social Distance
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Polycarp and his friends want to visit a new restaurant. The restaurant has nn tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from 11 to nn in the order from left to right. The state of the restaurant is described by a string of length nn which contains characters "1" (the table is occupied) and "0" (the table is empty).
Restaurant rules prohibit people to sit at a distance of kk or less from each other. That is, if a person sits at the table number ii, then all tables with numbers from i−ki−k to i+ki+k (except for the ii-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than kk.
For example, if n=8n=8 and k=2k=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=2k=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 ss that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string ss.
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=6n=6, k=1k=1, s=s= "100010", then the answer to the problem will be 11, since only the table at position 33 can be occupied such that the rules are still satisfied.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test. Then tt test cases follow.
Each test case starts with a line containing two integers nn and kk (1≤k≤n≤2⋅1051≤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 ss of length nn 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 kk.
The sum of nn for all test cases in one test does not exceed 2⋅1052⋅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 00.
Example
input
Copy
6 6 1 100010 6 2 000000 5 1 10101 3 1 001 2 2 00 1 1 0
output
Copy
1 2 0 1 1 1
Note
The first test case is explained in the statement.
In the second test case, the answer is 22, 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.
=========================================================================
贪心,从前往后遍历即可,能加就加,加的条件是左右k距离没有1
判断区间内是否有1的方法是前缀和,但本题是动态修改的,也就是当前位置前面是否有1是需要根据之前修改情况也判断的,当然,右边是否有1是完全可以利用前缀和来判断的,至于前面的,我们设置一个pre变量即可,每当遇见1就修改,值得注意的是,我们新添进去的1也不能忘记修改pre
#include<iostream>
#include<cstdio>
#include<cstring>
# include<iomanip>
#include<algorithm>
#define mo 998244353;
using namespace std;
typedef long long int ll;
int sum[200000+10];
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
int n,k;
cin>>n>>k;
cin>>s;
s=" "+s;
for(int i=1;i<=n;i++)
{
sum[i]=sum[i-1]+s[i]=='1';
}
int pre=-1e9;
int ans=0;
for(int i=1;i<=n;i++)
{
if(s[i]=='1')
{
pre=i;
}
else if(s[i]=='0')
{
if(pre+k<i&&sum[min(n,i+k)]-sum[i]==0)
{
ans++;
pre=i;
}
}
}
cout<<ans<<endl;
}
return 0;
}边栏推荐
- sklearn 笔记 TSNE
- Ferritin particle-loaded raltitrexed/pemetrexed/sulfadesoxine/adamantane (scientific research reagent)
- Web3中值得关注的基础设施
- 洛谷 P1629 邮递员送信 (三种最短路)
- 饿了么-机构树单选
- 详叙c中的分支与循环
- 电信保温杯笔记——《统计学习方法(第二版)——李航》第17章 潜在语义分析
- Single-click to cancel the function
- 【go】依赖注入
- Before implementing MES management system, these three questions to consider
猜你喜欢

win7开机有画面进系统黑屏怎么办

用示波器揭示以太网传输机制

Knowledge map Knowledge Graph

姜还是老的辣,看看老战哥的老底儿和严谨劲儿

【go】依赖注入

爱丁堡大学最新《因果机器学习: 医疗健康与精准医疗应用》2022综述

爬虫基本原理介绍、实现以及问题解决

每日一R「03」Borrow 语义与引用

Pt/CeO2 monatomic nanoparticles enzyme | H - rGO - Pt @ Pd NPs enzyme | carbon nanotube load platinum nanoparticles peptide modified nano enzyme | leukemia antagonism FeOPtPEG composite nano enzyme

apr_thread使用内存之谜
随机推荐
论文解读(g-U-Nets)《Graph U-Nets》
ansible各个模块的详解和使用
Transferrin (TF) Modified Paclitaxel (PTX) Liposomes (TF-PTX-LP) | Transferrin (Tf) Modified Curcumin Liposomes
机器学习笔记:t-SNE
通用线程:POSIX 线程详解,第 2部分
ctfshow-osint
@Autowired annotation --required a single bean, but 2 were found causes and solutions
How to submit a PR?【OpenHarmony Growth Plan】【OpenHarmony Open Source Community】
面向未来的 IT 基础设施管理架构——融合云(Unified IaaS)
用汇编带你看Golang里到底有没有值类型、引用类型
Pt/CeO2 monatomic nanoparticles enzyme | H - rGO - Pt @ Pd NPs enzyme | carbon nanotube load platinum nanoparticles peptide modified nano enzyme | leukemia antagonism FeOPtPEG composite nano enzyme
Knowledge map Knowledge Graph
“蔚来杯“2022牛客暑期多校训练营7 F
Single-click to cancel the function
Apache DolphinScheduler 3.0.0 正式版发布!
ctfshow-osint
【Windows】你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问,这些策略可帮助保护你的电脑
win7开机有画面进系统黑屏怎么办
Getting started with kuberentes Auditing
2021 CybricsCTF