Black & White(尺取)
链接:https://ac.nowcoder.com/acm/contest/893/F
来源:牛客网
* 第一行一个整数 T ,表示接下来有 T 个样例。
* 首先输入n,m,表示S串的长度n和操作次数m,其中1≤n≤1000001≤n≤100000,0≤m≤10000≤m≤1000;
* 接下来输入一个长度为n的字符串S。
输出描述:
一个整数,表示题面上描述的最大价值。
说明
第一个串翻转第三个位置,00001的价值为4;第二个串翻转第一个位置,11的价值为2。
思路:记录每个串中1的位置和0的位置,然后尺取
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath> const int maxn=1e5+;
typedef long long ll;
using namespace std;
string str;
int l[maxn];
int main()
{
int T;
cin>>T;
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
cin>>str;
l[]=-;
int cnt=;
for(int t=;t<n;t++)
{
if(str[t]=='')
{
l[cnt++]=t;
}
}
l[cnt++]=n;
// cout<<cnt<<endl;
if(m>=cnt-)
{
printf("%d\n",n);
continue;
}
int ans=;
for(int t=;t<cnt-m;t++)
{
ans=max(ans,l[t+m]-l[t-]-);
}
cnt=;
l[]=-;
for(int t=;t<n;t++)
{
if(str[t]=='')
{
l[cnt++]=t;
}
}
l[cnt++]=n;
if(m>=cnt-)
{
printf("%d\n",n);
continue;
}
for(int t=;t<cnt-m;t++)
{
ans=max(ans,l[t+m]-l[t-]-);
}
cout<<ans<<endl; }
return ;
}
Black & White(尺取)的更多相关文章
- Gym 100703I---Endeavor for perfection(尺取)
题目链接 http://codeforces.com/problemset/gymProblem/100703/I Description standard input/outputStatement ...
- NOJ 1072 The longest same color grid(尺取)
Problem 1072: The longest same color grid Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit in ...
- hdu 4123 Bob’s Race 树的直径+rmq+尺取
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)
题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序 ...
- poj2566尺取变形
Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...
- poj2100还是尺取
King George has recently decided that he would like to have a new design for the royal graveyard. Th ...
- hdu 6231 -- K-th Number(二分+尺取)
题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...
- Codeforces 939E Maximize! (三分 || 尺取)
<题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...
- cf1121d 尺取
尺取,写起来有点麻烦 枚举左端点,然后找到右端点,,使得区间[l,r]里各种颜色花朵的数量满足b数组中各种花朵的数量,然后再judge区间[l,r]截取出后能否可以供剩下的n-1个人做花环 /* 给定 ...
随机推荐
- 电力规约DL/T 654 2007多功能电表通信协议 调试工具
DL/T 654 2007多功能电表通信协议 调试工具 最近调试DL/T654 2007电力规约,在网上找到一个比较好用的工具,分享给大家,希望对大家有帮助. CSDN需要积分,我传百度网盘了, 内含 ...
- Python爬虫开发:反爬虫措施以及爬虫编写注意事项
- spring boot 中使用spring security阶段小结
1 项目结构图 2 AnyUserDetailsService package com.fengyntec.config; import com.fengyntec.entity.UserEntity ...
- GitLab 转让所有者权限
GitLab 转让所有者权限 没有查看文档,自己测试了一下,可能会有纰漏,谨慎阅读 私人项目的所有者 私人项目没有所有者权限,可以认为项目命名空间就是所有者. 私人项目http://xxxxxxxxx ...
- 使用Spock 单元测试
一.什么是Spock Spock 是一个测试框架,甚至可以说是一门语言他是基于Groovy开发的.它的语法完全遵循 BDD(行为驱动开发) 风格的结构.它是基于 Junit test runner 上 ...
- Java—io流之打印流、 commons-IO
打印流 打印流根据流的分类: 字节打印流 PrintStream 字符打印流 PrintWriter /* * 需求:把指定的数据,写入到printFile.txt文件中 * * 分析: * 1, ...
- C#LeetCode刷题之#884-两句话中的不常见单词(Uncommon Words from Two Sentences)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3816 访问. 给定两个句子 A 和 B . (句子是一串由空格分 ...
- 性能分析(5)- 软中断导致 CPU 使用率过高的案例
性能分析小案例系列,可以通过下面链接查看哦 https://www.cnblogs.com/poloyy/category/1814570.html 前言 软中断基本原理,可参考这篇博客:https: ...
- LeetCode 309 Best Time to Buy and Sell Stock with Cooldown 解决方案
题目描述 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 . 设计一个算法计算出最大利润.在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票): 你不能同时参与多笔 ...
- CSAPP =1= 计算机系统漫游
思维导图 预计阅读时间:15min 阅读书籍 <深入理解计算机系统> 参考视频 [精校中英字幕]2015 CMU 15-213 CSAPP 深入理解计算机系统 课程视频 参考文章 < ...