Codeforces Round #354 (Div. 2) C. Vasya and String 二分
C. Vasya and String
题目连接:
http://www.codeforces.com/contest/676/problem/C
Description
High school student Vasya got a string of length n as a birthday present. This string consists of letters 'a' and 'b' only. Vasya denotes beauty of the string as the maximum length of a substring (consecutive subsequence) consisting of equal letters.
Vasya can change no more than k characters of the original string. What is the maximum beauty of the string he can achieve?
Input
The first line of the input contains two integers n and k (1 ≤ n ≤ 100 000, 0 ≤ k ≤ n) — the length of the string and the maximum number of characters to change.
The second line contains the string, consisting of letters 'a' and 'b' only.
Output
Print the only integer — the maximum beauty of the string Vasya can achieve by changing no more than k characters.
Sample Input
4 2
abba
Sample Output
4
Hint
题意
有一个长度为n的字符串,你可以改变最多k次,问你最长的全是一样字符的串是多长
这个字符串只含有a和b字符
题解:
枚举结尾,然后暴力二分就好了
用前缀和预处理一下,那么前缀和之差,就是你需要改变的个数……
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int n,k,sum[maxn];
char s[maxn];
int main()
{
scanf("%d%d",&n,&k);
scanf("%s",s+1);
for(int i=1;i<=n;i++)
{
sum[i]=sum[i-1];
if(s[i]=='a')sum[i]++;
}
int ans = 0;
for(int i=1;i<=n;i++)
{
int l = 1,r = i,Ans = i;
while(l<=r)
{
int mid = (l+r)/2;
if(sum[i]-sum[mid-1]<=k)r=mid-1,Ans=mid;
else l=mid+1;
}
if(sum[i]-sum[Ans-1]<=k)
ans=max(ans,i-Ans+1);
}
for(int i=1;i<=n;i++)
{
sum[i]=sum[i-1];
if(s[i]=='b')sum[i]++;
}
for(int i=1;i<=n;i++)
{
int l = 1,r = i,Ans = i;
while(l<=r)
{
int mid = (l+r)/2;
if(sum[i]-sum[mid-1]<=k)r=mid-1,Ans=mid;
else l=mid+1;
}
if(sum[i]-sum[Ans-1]<=k)
ans=max(ans,i-Ans+1);
}
cout<<ans<<endl;
}
Codeforces Round #354 (Div. 2) C. Vasya and String 二分的更多相关文章
- Codeforces Round #354 (Div. 2)——C. Vasya and String(尺取)
C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #354 (Div. 2)-C. Vasya and String,区间dp问题,好几次cf都有这种题,看来的好好学学;
C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #354 (Div. 2) C. Vasya and String
题目链接: http://codeforces.com/contest/676/problem/C 题解: 把连续的一段压缩成一个数,对新的数组求前缀和,用两个指针从左到右线性扫一遍. 一段值改变一部 ...
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分
C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- 构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root
题目传送门 /* 构造水题:对于0的多个位数的NO,对于位数太大的在后面补0,在9×k的范围内的平均的原则 */ #include <cstdio> #include <algori ...
- Codeforces Round #184 (Div. 2) E. Playing with String(博弈)
题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...
- Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点
// Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
随机推荐
- vue总结 03过滤器
过滤器 Vue.js 允许你自定义过滤器,可被用于一些常见的文本格式化.过滤器可以用在两个地方:双花括号插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持).过滤器应该被添加在 JavaS ...
- MongoDB-MongoDB重装系统后恢复
重装系统后,把原mongoDB安装目录和原mongoDB的data目录拷贝到新硬盘的D盘上. 恢复的方法如下. 1.D:\Mongodb里放着mongod.cfg和data C:\Users\Admi ...
- ls和cd命令详解
ls命令 命令功能: 列出当前目录下或者指定目录下的所有文件和目录,ls是list的缩写. 命令语法: ls [选项][目录名 ] #注:[ ]中的内容为非必选项 命令选项: | 选项 | 含义 ...
- js面试题之求数组最值
今天继续分享js常见的面试题,求数组最大值,最小值,这里列举4种常见解法,还有其他方法也可以实现,读者知道可以私信我,我将把意见列举到博客中,欢迎提出意见. 第一种,利用数组排序 var arr=[3 ...
- DEDECMS去除后门隐患和漏洞以及冗余代码的方法
链接:http://jingyan.baidu.com/article/4d58d541195bdb9dd4e9c029.html 工具/原料 织梦网站管理系统 sublime编辑器 方法/步骤 第一 ...
- MySQL基础 - 视图
创建视图: 假设要将posts表的前十条数据作为视图 mysql> CREATE VIEW view_test AS SELECT * FROM POSTS LIMIT 10; 使用: 可以把视 ...
- [java笔记]动态数组
private int count;//计数器 private int ary[] = new int [3]; if(count >= ary.length){ //数组动态扩展 int ne ...
- Centos之其他帮助命令
选项帮助 命令 -help 获取命令选项的帮助 例如 ls --help 我们会发现用这种方式查看帮助命令 居然还有中文解释: 详细命令帮助info info 命令 -回车:进入子帮助页面(带有*号标 ...
- (二)HtmlUnit 使用
第一节: htmlunit 模拟浏览器请求 第二节: htmlunit 获取指定元素 第三节: htmlunit 使用代理 IP 第四节: htmlunit 取消 css,javascript 支持 ...
- Java工程师知识图谱
一.Java工程师知识图谱(思维导图版) 二.Java工程师知识图谱(图文版) 三.Java工程师知识图谱(文字版) http://note.youdao.com/noteshare?id=615da ...