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 input
outputstandard output
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.
Input
The first line contains one integer n (2≤n≤105) — 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.
Output
Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.
Examples
inputCopy
10
GGGSGGGSGG
outputCopy
7
inputCopy
4
GGGG
outputCopy
4
inputCopy
3
SSS
outputCopy
0
Note
In the first example Vova has to swap trophies with indices 4 and 10. Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is 7.
In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is 4.
In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than 0.
题意:
思路:
标程写的挺好的,推荐一下吧。
细节见代码:
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int main() {
cin >> n >> s;
vector <int> l(n), r(n);
for(int i = 0; i < n; ++i){
if(s[i] == 'G'){
l[i] = 1;
if(i > 0) l[i] += l[i - 1];
}
}
for(int i = n - 1; i >= 0; --i){
if(s[i] == 'G'){
r[i] = 1;
if(i + 1 < n) r[i] += r[i + 1];
}
}
int res = 0;
int cntG = 0;
for(int i = 0; i < n; ++i)
cntG += s[i] == 'G';
for(int i = 0; i < n; ++i){
if(s[i] == 'G') continue;
int nres = 1;
if(i > 0) nres += l[i - 1];
if(i + 1 < n) nres += r[i + 1];
res = max(res, nres);
}
res = min(res, cntG);
if(cntG == n) res = cntG;
cout << res << endl;
return 0;
}
Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies (贪心+字符串)的更多相关文章
- 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
传送门 https://www.cnblogs.com/violet-acmer/p/10035971.html 题意: Vova有n个奖杯,这n个奖杯全部是金奖或银奖,Vova将所有奖杯排成一排,你 ...
- Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】
传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limit per test 2 ...
- Educational Codeforces Round 55 (Rated for Div. 2) A/B/C/D
http://codeforces.com/contest/1082/problem/A WA数发,因为默认为x<y = = 分情况讨论,直达 or x->1->y or x-& ...
- Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))
C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))
A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency
E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...
- Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph
D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...
- Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition
C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...
随机推荐
- CSS注
1.css3内容上下左右居中 .box { display:-moz-box; -moz-box-pack:center; -moz-box-align:center; display:-webkit ...
- fastJson对象转字符串首字母小写问题
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- jpg图片转换为yuv
ffmpeg -i d:/demo.jpg -s 400x500 -pix_fmt yuvj420p d:/test.yuv ffmpeg -i http://www.test.com/test.fl ...
- ubuntu通过windows下的ccproxy代理上网
网上教程很多,需要注意的是将ubuntu的ip和windows的Ip设置到同一个网段,即子网掩码是1的对应的部分要相同.由于没有配置到同一个网段,折腾了我好久.
- 阶段3 1.Mybatis_12.Mybatis注解开发_7 Mybatis注解开发一对多的查询配置
一对多的配置,一个用户对应多个账户 需要在Accout里面增加根据用户的uid查询的方法 在user里面指定子一对多的查询配置 换行显示 测试 把这里注销掉.测试延迟加载,代码注释掉后,延迟加载就没有 ...
- 2.转发。基于itchat的微信消息同步机器人
原文:https://www.jianshu.com/p/7aeadca0c9bd# 看到了该网址有基于itchat的微信消息同步机器人,转过来继续研究.以下是转过来的: 最近 全栈数据工程师养成攻略 ...
- 【ABAP系列】SAP ABAP 控制ALV单元格编辑后获取新的数值
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 控制ALV单元 ...
- 20191127 Spring Boot官方文档学习(5)
5.Spring Boot Actuator:可投入生产的功能 Spring Boot包含许多其他功能,可帮助您在将应用程序投入生产时监控和管理您的应用程序.您可以选择使用HTTP端点或JMX管理和监 ...
- Binary Tree Level Order Traversal(二叉树广度优先遍历或逐层遍历)
来源:https://leetcode.com/problems/binary-tree-level-order-traversal Given a binary tree, return the l ...
- 【Linux 环境搭建】Ubuntu下安装tftp
1.安装软件 sudo apt-get install tftp-hpa tftpd-hap xinetd 2.修改配置文件 sudo vim /etc/default/tftpd-hpa ...