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 ...
随机推荐
- 从Python到Web开发
基础部分: 1-编程基础及Python环境部署 2-Python基础语法-内存管理-运算符-程序控制 3-Python内置结构-列表 4-Python数据类型之元组-字符串 5-python的封装与结 ...
- 串口流控制详解(CTS/RTS,DTR/DSR)
1 首先看下关于流控相关的几个端口的解释如下图 除了必要的地(GND)要连接外,其它连如下 步骤阅读 2 计算机和猫(MODEM)的连接 步骤阅读 步骤阅读 3 计算机和非猫的连接(null mod ...
- 打包egg
scrapyd-deploy -p chahao -v 1.0 --build-egg chahao.egg
- 从一个局长使用BS系统的无奈看测试点
今天我点名买了个B/S系统,听说只要有浏览器就能用.我最讨厌装客户端了,用浏览器就是方便啊. 下面就是我使用这个系统碰到的麻烦事: 我登录失败的时候没有任何提示,这没什么,反正提示也只是说失败…… 进 ...
- Git如何设置多个用户
前言 由于我们在使用GitHub时,通常不希望带有公司信息,所以需要独立的Git账户来提交练习代码,本文记录一下如何配置多个Git账户并创建公钥 正文 1.首先进入~/.ssh文件夹 2.然后创建一个 ...
- vue2.0组件之间的传值
1.父子组件--props props:需要注意的是,虽然的是单向的数据流,但是如果传递的是数组或是对象这样的引用类型,子组件数据变化,父组件的数据通也会变化 子组件代码 <template&g ...
- (四)MyBatis关系映射
第一节:一对一关系实现 需要实现一对一的关系,首先我们有两张表,t-addree和t_student. CREATE TABLE `t_address` ( `id` ) NOT NULL AUTO_ ...
- 关于ARM指令那些你必须知道的东西
1.32位ARM指令每一位都有其作用,具体如下: 低12为第二操作数, 12~15位为目的寄存器, 16~19位为第一操作数, 20~27就是操作码, 28~31就是条件域. 2.多寄存器load和s ...
- 浅谈DDD
从遇到问题开始 当人们要做一个软件系统时,一般总是因为遇到了什么问题,然后希望通过一个软件系统来解决. 比如,我是一家企业,然后我觉得我现在线下销售自己的产品还不够,我希望能够在线上也能销售自己的产品 ...
- hdu 1007 N个点中输出2点的最小距离的一半
分治法 Sample Input20 01 121 11 13-1.5 00 00 1.50 Sample Output0.710.000.75 # include <iostream> ...