当前位置:网站首页>6.26cf simulation game d: black and white questions
6.26cf simulation game d: black and white questions
2022-06-29 16:43:00 【bj_ hacker】
6.26CF Simulation game D: Black and white questions
Title Description
link
6.26CF Simulation game D topic
A word description
D. Black and white bar
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.
You have a length of n The checkered paper . Each square is dyed white or black .
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?
If we want to get a continuation on this piece of checkerboard k A black grid , We should at least re dye a few white plaids into black plaids ?
If the input data is such that a segment of k consecutive black cells already exists, then print 0. If there is already a continuous section on this checkered paper k A black grid , Output 0.
Input
The first line contains an integer t (1≤t≤104) — the number of test cases.
The first line contains an integer t (1≤t≤104) — Number of representative test data groups .
Next, descriptions of t test cases follow.
Here are t Group test data .
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.
The first line contains two integers n and k (1≤k≤n≤2⋅105). The second line contains n Letters ‘W’ ( white ) and ‘B’ ( black ).
It is guaranteed that the sum of values n does not exceed 2⋅105.
Data assurance n The sum does not exceed 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.
Each set of test data outputs an integer representing the answer .
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’.
For the first set of samples ,s=“BBWBW” And k=3, Only will s3 You can get it by dyeing it black s=“BBBBW”. This string contains consecutive k=3 individual ‘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’. For the second set of examples s=“BBWBW” And k=5, Need to put s3 and s5 obtain s=“BBBBB”. The results include k=5 individual ‘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’.
For the third set of samples ,s The requirements have been met .
Topic analysis
- To make k individual B All possible cases in succession are n-k+1 Kind of ( From the first place 1 To n-k+1)
- We just need to know this n-k+1 How many are there in the species W, Taking the minimum value is the final answer
- There is an algorithm that supports O(n) Algorithm of time complexity , The prefix and
- The first i Bit start length is k The string of is f[i+k-1]-f[i-1] individual W
Code implementation
#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;
}
边栏推荐
- 又拍云 Redis 的改进之路
- 南京大学:新时代数字化人才培养方案探讨
- The understanding of industrial Internet is directly related to how we view it
- [day 28] given a string s, please judge whether it is a palindrome string | palindrome judgment
- 6.25AtCoderABC257E - Addition and Multiplication 2题解
- Basics | draw arcs in the physics engine
- 连续10年霸榜第一?程序员「最常用」的编程语言是它?
- [untitled]
- Us chips are hit hard again, and Intel may be defeated by TSMC and reduced to the third place in the world
- Review of mathematical knowledge: curve integral of type I
猜你喜欢

Selenium 凭什么成为 Web 自动化测试的首选?(内附源码)

Real test = "half product + Half development"?

A tour of gRPC:02 - 从proto生成代码

关于XAMPP无法启动mysql数据库

C winfrom chart chart control bar chart and line chart

Interviewer: tell me about the MySQL transaction isolation level?

Comment configurer logback? 30 minutes pour apprendre à coder et à frapper tard.

MySQL基础——多表查询

After studying this series of notes about software testing, it is a "bonus" to enter the factory

Privacy computing helps secure data circulation and sharing
随机推荐
穩定幣風險狀况:USDT 和 USDC 安全嗎?
Is it safe to open a compass account and speculate in stocks? How individuals open accounts for stock trading
使用kalibr标定工具进行单目相机和双目相机的标定
实践 | 脚本错误量极致优化-让脚本错误一目了然
InheritableThreadLocal 在线程池中进行父子线程间消息传递出现消息丢失的解析
高级性能测试工程师面试必问十大问题
DAP large screen theme development description
[untitled]
元代理模型可迁移对抗攻击
自己实现一个ThreadLocal
机器学习8-人工神经网络
Graduates are confused and middle-aged people are anxious. How can the career path become wider and wider?
To solve the stubborn problem of Lake + warehouse hybrid architecture, Star Ring Technology launched an independent controllable cloud native Lake warehouse integrated platform
A tour of gRPC:02 - 从proto生成代码
C winfrom chart chart control bar chart and line chart
或许再过两年,ASML将可以自由给中国供应EUV光刻机
New feature of C11 - Auto and decltype type type indicators
Privacy computing helps secure data circulation and sharing
Time format GTM to Beijing time
Picture and text show you how to thoroughly understand the atomicity of MySQL transaction undolog