6.26CF模拟赛D:黑白条题题解

  • 题目描述
    • 链接
    • 文字描述
  • 题目分析
  • 代码实现

题目描述

链接

6.26CF模拟赛D题

文字描述

D. 黑白条
time limit per test2 s.
memory limit per test256 MB
inputstandard input
outputstandard output
You have a stripe of checkered paper of length n. Each cell is either white or black.

你有一条长度为 n 的方格纸。每个方格被染上白色或者黑色。

What is the minimum number of cells that must be recolored from white to black in order to have a segment of k consecutive black cells on the stripe?

如果我们想在这条方格纸上得到一段连续 k 个黑格子,我们至少要把几个白格子重新染成黑格子?

If the input data is such that a segment of k consecutive black cells already exists, then print 0. 如果这条方格纸上已经存在一段连续 k 个黑格子,输出 0。

Input
The first line contains an integer t (1≤t≤104) — the number of test cases.

第一行包含一个整数 t (1≤t≤104) — 代表测试数据组数。

Next, descriptions of t test cases follow.

以下是 t 组测试数据。

The first line of the input contains two integers n and k (1≤k≤n≤2⋅105). The second line consists of the letters ‘W’ (white) and ‘B’ (black). The line length is n.

第一行包含两个整数 n 和 k (1≤k≤n≤2⋅105)。第二行包含 n 个字母 ‘W’ (白) 和 ‘B’ (黑)。

It is guaranteed that the sum of values n does not exceed 2⋅105.

数据保证 n 之和不超过 2⋅10^5。

Output
For each of t test cases print an integer — the minimum number of cells that need to be repainted from white to black in order to have a segment of k consecutive black cells.

每组测试数据输出一个整数代表答案。

Example
inputCopy
4
5 3
BBWBW
5 5
BBWBW
5 1
BBWBW
1 1
W
outputCopy
1
2
0
1
Note
In the first test case, s=“BBWBW” and k=3. It is enough to recolor s3 and get s=“BBBBW”. This string contains a segment of length k=3 consisting of the letters ‘B’.

对于第一组样例,s=“BBWBW” 及 k=3,只将 s3 染黑即可得到 s=“BBBBW”。这个字符串包含连续 k=3 个 ‘B’。

In the second test case of the example s=“BBWBW” and k=5. It is enough to recolor s3 and s5 and get s=“BBBBB”. This string contains a segment of length k=5 consisting of the letters ‘B’. 对于第二组样例 s=“BBWBW” 及 k=5,需要将 s3 和 s5 得到 s=“BBBBB”。结果包含 k=5 个 ‘B’。

In the third test case of the example s=“BBWBW” and k=1. The string s already contains a segment of length k=1 consisting of the letters ‘B’.

对于第三组样例,s 已经满足要求。

题目分析

  1. 要使k个B连续所有可能的情况有n-k+1种(从首位1到n-k+1)
  2. 我们只需要知道这n-k+1种里有多少个W,取最小值就是最终的答案
  3. 有一种算法可以支持O(n)时间复杂度的算法,前缀和
  4. 第i位开头长度为k的字符串有f[i+k-1]-f[i-1]个W

代码实现

#include<bits/stdc++.h>
using namespace std;const int maxn=2e5+10;
int t,n,k,ans;
char a[maxn];
int f[maxn];
int main(){scanf("%d",&t);while(t--){ans=maxn;scanf("%d%d",&n,&k);for(int i=1;i<=n;i++){scanf(" %c",&a[i]);f[i]=f[i-1];if(a[i]=='W')f[i]++;}for(int i=1;i<=n-k+1;i++){int x=f[i+k-1]-f[i-1];ans=min(ans,x);}printf("%d\n",ans);}return 0;
}

6.26CF模拟赛D:黑白条题题解相关推荐

  1. 2022 第十四届蓝桥杯模拟赛第一期(题解与标程)

    第十四届蓝桥杯模拟赛第一期 1. 二进制位数 问题描述 答案提交 参考答案 2. 晨跑 问题描述 答案提交 参考答案 3. 调和级数 问题描述 答案提交 参考答案 程序验证 4. 山谷 问题描述 答案 ...

  2. 2020蓝桥杯B 组省赛计蒜客模拟赛(一)题解

    2020蓝桥杯省赛 B 组计蒜客模拟赛(一)目录 试题 A:有趣的数字(结果填空) 试题 B:爬楼梯(结果填空) 试题 C:七巧板(结果填空) 试题 D:苹果(结果填空) 试题 E:方阵(结果填空) ...

  3. 【第十四届蓝桥杯】第三期官方校内模拟赛B组C++题解(已修正完毕,均可AC100%)

    文章目录 写在前面 一.字母数(AC100%) 题目描述 解题报告 1.大体思路 2.代码详解 二.列名(AC100%) 题目描述 解题报告 1.大体思路 2.代码详解 三.特殊日期(AC100%) ...

  4. 2021年CCCC天梯赛 【部分题题解】

    天梯赛有三个level,第一个level基本就是语法题,第二个level是基础算法和STL库的一些应用. 第三个level就是一些难的算法. L3的题都不是太会,有思路但是写不出来. 目录 L1 人与 ...

  5. 【NOI P模拟赛】序列题 (二分)

    题面 题解 --WQS二分 想到这个这题就完了. 赛时没想到这个你就完了. 时间复杂度 O ( n log ⁡ a ) O(n\log a) O(nloga) 不难发现这题有凸性,可以WQS二分. 我 ...

  6. 2020 计蒜客蓝桥杯省赛 B 组模拟赛(一)题解4.苹果

    每日刷题(八) 苹果(结果填空题) 极端假设先考虑3的倍数的篮子,如下图所示 共可以分给44个同学 如果细致考虑这题用枚举法也是可以强行解出的 答案是62 如果编写代码也是可以完成这题的,但会比较复杂 ...

  7. 中医药暑假训练赛三 c题 题解 (多重背包问题)

    原题: Description 主角kirito是使用世界首款完全潜行游戏"刀剑神域(Sword Art Online)"的玩家.曾经很幸运的参与过封闭测试,并买下正式版的kiri ...

  8. 2005年全国信息学分区联赛模拟赛 猫猫的小鱼 题解

    第一题 猫猫的小鱼 提交文件:catfish.pas/c/cpp 输入文件:catfish.in 输出文件:catfish.out 猫猫是丛林里很多动物心中的天使,她为此十分自豪.猫猫最爱吃鱼了,她每 ...

  9. 【NOIP模拟赛】迷路 expand 题解

    Description Marah要出去买菜,但不小心迷路了,它记得所有菜店的坐标,也知道它现在的坐标.请你帮帮她,找 到一条买完菜的路吧.它已经急得快哭了,它想要买完菜回家.因此它需要你找到一条最短 ...

最新文章

  1. python能做游戏吗-python能做游戏吗
  2. 实时获取vuex更新的新数据_京东手机销量实时数据更新 荣耀Play4T Pro位列第一
  3. Johnny and Another Rating Drop CodeForces - 1362C(规律)
  4. Java多线程_阻塞队列
  5. 前端性能优化之性能测试
  6. Configuring SharePoint 2010 and ADFS v2 End to End-摘自网络
  7. MLP、RBF、SVM网络比较及其应用前景
  8. HashKey TokenGazer | 去中心化身份(DID)研究报告
  9. 计算机照片文件大小,怎么把照片文件大于30k
  10. NC单据模板公式(6大类)
  11. HTML_旅行志界面
  12. CSM300调试心得
  13. 企业如何选择短信平台
  14. 多项式时间 P问题 NP问题
  15. 顶级在线图片处理工具Photopea
  16. EZchip(Tilera) SDN和NFV解决方案
  17. idea jar包在x-shell 上执行,并使用jara -jar
  18. PowerMock与Mockito使用教程
  19. QD77MS4 RD77MS4 运动控制模块凸轮曲线样例程序
  20. JavaScript 练手小案例:超级简单又炫酷的图片手风琴效果

热门文章

  1. 手机装linux无root权限,linux无root权限安装screen(示例代码)
  2. raid卡直通模式会走缓存吗_磁盘阵列 RAID 技术如何保护数据
  3. kalman滤波的matlab,kalman滤波matlab实现
  4. 零跑C11斩获大奖,带来了极致的挑战
  5. USB Type-C PD快充简介
  6. LoadRunner--并发测试(多用户)
  7. Cesium深入浅出之视频投影
  8. C语言_递归_计算x的y次方
  9. 酷我音乐盒去广告(续)
  10. 第二章 pandas基础