C - Social Distance

CodeForces - 1367C

题目大意
有t组测试案例 ,输入n和k,n是桌子的数量,k是餐厅规定的每两个人之间的桌子数量,以字符串为例,n=8,k=2,字符串“10010000” 那么在满足餐厅规定的前提下可以占用一张桌子使其变成“10010010”,现在目的是找到满足餐厅规定下最多可以占用桌子的数量是多少?
思路
首先我们可以思考每两个1之间需要有k个0,那么我们可以从数组1的位置开始遍历,如果这个数是‘1’,那么它后面至少应该有k个‘0’,要在满足后面k个位置都是0的基础上,然后再继续从这个数组的位置加上k在加上1的位置开始遍历,如果这个位置的数为‘0’,那么可以把‘0’,换成‘1’,然后继续上面的操作。
代码如下

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
char a[N];
int main()
{int t,n,k;scanf("%d\n",&t);while(t--){scanf("%d%d\n",&n,&k);gets(a);    //输入字符串int c=0,d,j;for(int i=0; i<n;){d=0;for( j=i;j<i+k+1;j++){if(a[j]=='1'){i=j+k+1;d=1;break;}if(j==n-1){break;}}if(d!=1)   //这里d!=1,说明前一个1开始k个位置都是0!!!{i=i+k+1;c++;   //这里用c记录可以添加的数量}}cout<<c<<"\n";}return 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.

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.

C - Social Distance CodeForces - 1367C相关推荐

  1. 基于yolov5的行人检测跟踪与社交距离预测 (pedestrian detection and social distance prediction)

    代码.模型和示例DEMO下载地址: 下载地址 基本功能:通过距离分类人群的高危险和低危险距离. # 课程设计 # Social Distancing based on YOLOv5. 环境要求: Cy ...

  2. CF1367C - Social Distance

    题目链接 https://codeforces.com/problemset/problem/1367/C 题目描述 给出一个长为 nnn 的 010101 字符串和一个整数 kkk(1≤k≤n≤2× ...

  3. Social Distance

    Codeforces Round #783 (Div. 2)     B题 mm chairs are arranged in a circle sequentially. The chairs ar ...

  4. Codeforces Round #650 (Div. 3)(A-C)

    Short Substrings CodeForces - 1367A 思路:除了第一位和最后一位之外,剩下的两个之中取一个就行. 代码如下: #include<bits/stdc++.h> ...

  5. CodeForces - 1367

    CodeForces - 1367 A - Short Substrings 很显然 前两个字母都取 之后隔一个取一个char s[maxn],ans[maxn]; int t; int main() ...

  6. Codeforces Round #650 (Div. 3)(A-F1)题解

    A. Short Substrings 题解:按题意模拟即可 /*Keep on going Never give up*/ #pragma GCC optimize(3,"Ofast&qu ...

  7. Codeforces Round #783 (Div. 2) A-F

    A.Direction Change 讨论一下每种移动的情况即可.先交替,然后再走多的. 走多的过程需要交替在其他方向移动. #include <iostream> #include &l ...

  8. ai/ml_您本周应阅读的有趣的AI / ML文章(8月9日)

    ai/ml 分析与意见 (ANALYSIS AND OPINION) I want to start my weekly compilation list of AI/ML articles with ...

  9. opencv图像深度-1_OpenCV空间AI竞赛之旅(第1部分-初始设置+深度)

    opencv图像深度-1 OpenCV空间AI竞赛 (OpenCV Spatial AI Competition) Recently, the people at OpenCV launched th ...

最新文章

  1. vue 定时循环 setInterval
  2. Hadoop权威指南pdf
  3. ubuntu18.04 docker安装kafka
  4. flink shell的local模式(benv与senv的使用+处理报错的解决方案)
  5. Ajax实现动态及时刷新表格数据
  6. 课时28.假链接(掌握)
  7. Xcode代码提示联想功能失效,按command键点不进去类库,提示“?”
  8. python中order函数_order by排序
  9. 50年代黄岩师专_300多位30、40和50年代获得第一份技术工作的开发人员的故事
  10. 区块链公司从五大神秘术语理解区块链技术
  11. 合众朱光林:借O2O模式反馈企业才有价值
  12. python批量上传执行脚本_python 写的批量操作远程主机脚本(命令执行,上传、下载文件)...
  13. 2020年十大开源漏洞回顾
  14. MATLAB 不能保存变量问题及解决办法
  15. Python 最抢手、Java 最流行,前线程序员揭秘 2019 软件开发现状
  16. 计算机硬盘硬盘共享如何设置,两台电脑如何共享文件夹?Win10设置共享文件夹或共享磁盘的方法...
  17. Linux开机自动获取本机公网IP并发送至指定邮箱
  18. 【Niagara Vykon N4】物联网学习 02 照明控制
  19. 谷歌账号电子邮件怎么改_如何使用Google仅搜索您的电子邮件,事件和其他内容...
  20. crc16 ccitt的详细标准及其出处

热门文章

  1. datax数据迁移之从oracle往Mysql迁移
  2. 测试面试题 - GIT
  3. 谈谈对python中的日期、时间、时区的理解(1)
  4. 吐血!解决vmware中虚拟机开机黑屏的最终方法
  5. 关于对信号归一化后的频谱“消失”问题
  6. 微信小程序开发部署上线流程
  7. 关于overflow适配IE的问题
  8. web端上传图片的几种方式
  9. 微信小程序数组的合成方式
  10. 高级软件工程第九次作业:东理三剑客团队作业-随笔6