CF1082B Vova and Trophies 题解
CF1082B Vova and Trophies 题解
瞎搞题,推荐的,一看是道水题,就随手A了……
题目描述
Vova has won \(n\)trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row.
The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible — that means, to maximize the length of the longest such subsegment.
Help Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap.
输入格式
The first line contains one integer \(n( 2 \le n \le 10^5)\) — the number of trophies.
The second line contains \(n\) characters, each of them is either \(G\) or \(S\). If the $ i -th$ character is \(G\), then the \(i -th\) trophy is a golden one, otherwise it's a silver trophy.
输出格式
Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.
题解
单调队列
我的想法是从左往右搞一个前缀和,记录一下当前有几个\(G\)
然后我们思考,能让一个\(G\)去替换\(S\)的条件是什么:
是至少有一个\(G\)没被我们选中,如图
想要让倒数第三个格子变成\(G\),就必须有一个其他格子是\(G\),比如\(1\)号格子
那么可以分类讨论一哈子
- 如果当前有至少一个\(G\)没被选中,我们就可以容忍在某一段序列里出现少于等于1个\(S\)
如图,绿色是我们已选中的序列,序列外有一\(G\),则可以替换
2.如果当前已选中所有的\(G\),那我们就不能从某个地方抽调一个\(G\)过来替换掉\(S\)
若为该情况则不可以,因为已经没有多余的\(G\)来替换了
那么这个东西的实现我们就可以用一下单调队列
并不需要真的搞个队列,模拟一下就行
首先先设一个指针\(now\)指向零,然后读入
读入的时候如果读入的是\(G\),那么存为\(1\),否则存为\(0\)
这样前缀和数组\(sum[i]\)就可以统计出在\(i\)位置前有多少个\(G\)
\(sum[i] - sum[now]\)就是当前队列里有多少个\(G\)
我们比较与\(sum[i] - sum[now]\)与当前队列里的奖牌数,就可以根据上面说的两种情况做出特判
大水题……
#include<bits/stdc++.h>
using namespace std;
#define rint register int
int n, a[100010], sum[100010], now, ans, tot;
int main( void ){
cin >> n;
char ch;
for( rint i = 1; i <= n; i++ ){
cin >> ch;
if( ch == 'G' ){
a[i] = 1; tot++;
} else a[i] = 0;
sum[i] = sum[i - 1] + a[i];
}
for( rint i = 1; i <= n; i++ ){
if( sum[i] - sum[now] != tot ){
while( sum[i] - sum[now] < i - now - 1 && now <= i ) now++; ans = max( ans, i - now );
}else{
while( sum[i] - sum[now] < i - now && now <= i ) now++; ans = max( ans, i - now );
}
}
cout << ans;
return 0;
}
CF1082B Vova and Trophies 题解的更多相关文章
- Codeforces 1082B Vova and Trophies(前缀+后缀)
题目链接:Vova and Trophies 题意:给定长度为n的字符串s,字符串中只有G和S,只允许最多一次操作:任意位置的两个字符互换.求连续G的最长长度. 题解:维护pre和pr,nxt和nx. ...
- Educational Codeforces Round 55:B. Vova and Trophies
B. Vova and Trophies 题目链接:https://codeforc.es/contest/1082/problem/B 题意: 给出一个“GS”串,有一次交换两个字母的机会,问最大的 ...
- Codeforces 1082B Vova and Trophies 模拟,水题,坑 B
Codeforces 1082B Vova and Trophies https://vjudge.net/problem/CodeForces-1082B 题目: Vova has won nn t ...
- Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】
传送门:http://codeforces.com/contest/1082/problem/B B. Vova and Trophies time limit per test 2 seconds ...
- Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies (贪心+字符串)
B. Vova and Trophies time limit per test2 seconds memory limit per test256 megabytes inputstandard i ...
- Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies
传送门 https://www.cnblogs.com/violet-acmer/p/10035971.html 题意: Vova有n个奖杯,这n个奖杯全部是金奖或银奖,Vova将所有奖杯排成一排,你 ...
- Vova and Trophies CodeForces - 1082B(思维题)
Vova has won nn trophies in different competitions. Each trophy is either golden or silver. The trop ...
- B. Vova and Trophies 字符串预处理+思维+贪心
题意:给出一个字符串 只有G和S 可以交换任意两个位置的字符一次 问 最长的G的长度是多少 思路:预处理字符串 把相同的G粘成一个G 记一下数量 字符串变为 GSSGSGGSGSSG 相邻有一个S ...
- B. Vova and Trophies
链接 [https://codeforces.com/contest/1082/problem/B] 题意 给你一个包含GS的字符串,只允许交换一次任意不同位置的字符,问最长的连续G串是多少 分析 很 ...
随机推荐
- 吴裕雄--天生自然python学习笔记:编写网络爬虫代码获取指定网站的图片
我们经常会在网上搜索井下载图片,然而一张一张地下载就太麻烦了,本案例 就是通过网络爬虫技术, 一次性下载该网站所有的图片并保存 . 网站图片下载并保存 将指定网站的 .jpg 和 .png 格式的图片 ...
- mvn测试常用命令
-Dmaven.test.failure.ignore=true 测试报错忽略 例子: mvn package -DAPP_ENV=dev -Dmaven.test.failure.ignore=t ...
- win10安装navisworks失败,怎么强力卸载删除注册表并重新安装
一些搞设计的朋友在win10系统下安装navisworks失败或提示已安装,也有时候想重新安装navisworks的时候会出现本电脑windows系统已安装navisworks,你要是不留意直接安装n ...
- Django环境的搭建以及最简示例
一.环境的搭建 先安装pip yum install python-pip 安装失败: 安装epel扩展源 yum install epel-release 在安装pip 再利用pip安装django ...
- ranche2.0-CN
遵循以下两步,快速运行rancher2.0 Step1:准备一台linux主机 准备一台64位Linux主机(推荐centos7.5+),至少4GB内存.安装Kubernetes支持的Docker-c ...
- Lego:美团点评接口自动化测试实践
概述 接口自动化概述 众所周知,接口自动化测试有着如下特点: 低投入,高产出. 比较容易实现自动化. 和UI自动化测试相比更加稳定. 如何做好一个接口自动化测试项目呢? 我认为,一个“好的”自动化 ...
- Mysql数据导出到excel-基于python
阅读本文大概需要 6分钟. 数据除了在测试平台显示,有时候也会习惯用excel以及邮件展示,那么我们可以在测试平台上加一个导出excel功能,方便操作,下面介绍主要代码以及逻辑. 使用操作数据库的py ...
- 淘宝网-接口测试白皮书V0.1
<软件自动化测试开发> 出版了 淘宝(中国)软件有限公司 接口测试白皮书 V0.1 淘宝网平台测试组(qa.taobao.com) 淘宝网-接口测试白皮书 2 目录 1 接口测试的背 ...
- JNI 问题 wrong ELF class
使用JNI发现一个问题, wrong ELF class: ELFCLASS64)主要是机器是64位的OS,默认编译的.so是64位 而java设置的默认是32位 JDK, 所以会出现这个问题.那么就 ...
- 致歉Note7用户后,三星要还给世界下一个机皇
1月23日,三星电子在韩国首尔公布了Note7事件原因调查结果.此次认定过程精密而繁琐,最终结果发布距离三星宣布全球召回Note7已经有3个月. 相比事发之初各路信息甚嚣尘上,现在虽然还不能说已经 ...