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 ...
随机推荐
- 学习网站总结->
慕课大巴网:这是一个学习各类技术视频的网站 慕课大巴网点我-> 吾爱破解: 这是一个破解各类软件的网站 吾爱破解点我-> 鸠摩搜书:可以搜一些免费的书,我喜欢的都能搜到 鸠摩搜书点我-&g ...
- if(a==1) & if(1==a) 区别
[前提] 在公司参加项目时,看到前辈写if比较数值是否相等,经常会写 if(1==a) ,觉得有些奇怪,如是乎,将调查结果写下来记录一下. [结果] if(a==1) 与 if(1==a)是没有什么区 ...
- C# byte[] 转换16进制字符串
1.byte[] 转换16进制字符串 1.1 BitConverter方式 var str = DateTime.Now.ToString(); var encode = Encoding.UTF8; ...
- python类中的私有方法
假设有如下一个python类: class Foo(object): def __a(self): print "Bet you can't see me..." def bar( ...
- 网络协议之NAT穿透
NAT IPv4地址只有32位,最多只能提供大致42.9亿个唯一IP地址,当设备越来越多时,IP地址变得越来越稀缺,不能为每个设备都分配一个IP地址.于是,作为NAT规范就出现了.NAT(Networ ...
- Effective STL 学习笔记 Item 21:Comparison Function 相关
Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...
- Linux学习笔记之一及虚拟机的安装
学习Linux基础入门 学习实验楼Linux基础入门--学习笔记系列博客 第一节 Linux系统简介 Linux就是一个操作系统,操作系统在计算机系统中包括系统调用和内核两层.在简单了解了Linux的 ...
- 涨姿势系列之——内核环境下花式获得CSRSS进程id
这个是翻别人的代码时看到的,所以叫涨姿势系列.作者写了一个获取CSRSS进程PID的函数,结果我看了好久才看懂是这么一个作用.先放上代码 HANDLE GetCsrPid() { HANDLE Pro ...
- 毕设demo写好
2015年1月20日 14:41:47 阶段性暂停!! 把运行结果截图给了老师,老师说先整理下文档,然后下学期来了再部署到服务器上. 那么,下学期来了,估计也要把Epm和CR1000什么的搞好了. 先 ...
- C#socket编程序(三)
private void ReceiveData() { bool keepalive = true; Socket s = clientSocket; Byte[] buffer = new Byt ...