当前位置:网站首页>CF676C Vasya and String
CF676C Vasya and String
2022-06-26 13:57:00 【__ LazyCat__】
Ideas
Similar to the simple rule taking method , Separate the sequence with a,b To calculate .
To choose a Come on , We treat each b assignment 1 Representative needs 1 The price to get 1 The value of , Yes a The price is 0, This problem turns into a simple rule taking method .( At the greatest cost k The longest number of sequences can be obtained even if the answer ). Then on b Do the same thing , The answer is to take the maximum of two operations .
Code
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int cnt=0,n,k;
string ss;
cin>>n>>k>>ss;
int ans=0;
int s=1,w=0,sum=0;
a[1]=ss[0]=='a'?1:0;// Assign a value to the initial cost
for(int i=1;i<n;i++){
a[i+1]=(ss[i]=='a'?1:0);// Pretreatment cost
}
for(int i=1;i<=n;i++){
// Simple ruler method
sum++;w+=a[i];
while(s<=i&&w>k){
// cost w, Maximum price to pay k
sum--;w-=a[s++];
}
ans=max(ans,sum);
}
a[1]=ss[0]=='b'?1:0;// ditto
for(int i=1;i<n;i++){
a[i+1]=(ss[i]=='b'?1:0);
}
s=1,sum=0,w=0;
for(int i=1;i<=n;i++){
sum++;w+=a[i];
while(s<=i&&w>k){
sum--;w-=a[s++];
}
ans=max(ans,sum);
}
cout<<ans<<endl;
return 0;
}
边栏推荐
- MediaPipe手势(Hands)
- 三维向量的夹角
- Cloudcompare - Poisson reconstruction
- Design of PHP asymmetric encryption algorithm (RSA) encryption mechanism
- GO语言-管道channel
- Use performance to see what the browser is doing
- Variable declaration of typescript
- 泰山OFFICE技术讲座:使用字体粗体的四种情形
- I met the problem of concurrent programming in an interview: how to safely interrupt a running thread
- Zero basics of C language lesson 8: Functions
猜你喜欢
Calculate the distance between two points (2D, 3D)
Mongodb series window environment deployment configuration
Learn how to develop owl components by hand (7): practical use of owl projects
shell脚本详细介绍(四)
程序员必备,一款让你提高工作效率N倍的神器uTools
Es6: iterator
Some conclusions about Nan
Firewall introduction
Here document interaction free and expect automatic interaction
爱可可AI前沿推介(6.26)
随机推荐
7-2 picking peanuts
Postman自动化接口测试
Logical operation
虫子 STL string上
Cloudcompare - Poisson reconstruction
There are many contents in the widget, so it is a good scheme to support scrolling
Awk tools
Wechat applet - bind and prevent event bubble catch
Detailed introduction to shell script (4)
Wechat applet -picker component is repackaged and the disabled attribute is added -- above
防火墙介绍
7.consul service registration and discovery
Luogu p3426 [poi2005]sza-template solution
【HCSD应用开发实训营】一行代码秒上云评测文章—实验过程心得
Es common grammar I
KITTI Tracking dataset whose format is letf_ top_ right_ bottom to JDE normalied xc_ yc_ w_ h
7-2 a Fu the thief
Design of simple digital circuit traffic light
Is it safe to open a securities account? Is there any danger
Bug memory management