传送门:http://codeforces.com/contest/1082/problem/B

B. Vova and Trophies
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vova has won nn 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 nn (2≤n≤1052≤n≤105) — the number of trophies.

The second line contains nn characters, each of them is either G or S. If the ii-th character is G, then the ii-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
input

Copy
10
GGGSGGGSGG
output

Copy
7
input

Copy
4
GGGG
output

Copy
4
input

Copy
3
SSS
output

Copy
0
Note

In the first example Vova has to swap trophies with indices 44 and 1010. Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is 77.

In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is 44.

In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than 00.

题意概括:

给一串只含有 G 和 S 的字符串,有一次将两个字符对调位置的机会,求最长的连续 'G' 序列的长度。

解题思路:

想得太多系列,一开始用了二分搜索,debug到爆炸。

其实就是一个贪心:记录中间 ‘S’ 前面的 ‘G’的长度 和 后面 'G' 的长度,作和。取最大值 maxlen。最后答案和 总的‘G’数量 snt 进行一下比较,如果大了说明多加的,输出 snt,否则输出 maxlen。

AC code:

 //#include <cstdio>
//#include <iostream>
//#include <cstring>
//#include <algorithm>
//#include <cmath>
//#include <map>
//#define INF 0x3f3f3f3f
//#define LL long long
//using namespace std;
//const int MAXN = 1e5+10;
//char str[MAXN];
//int N, snt;
//
//bool check(int len)
//{
// bool flag = false;
// int sum = 0, k = 0;
// int lst = 0;
// for(int i = 0; i < N; i++){
// if(str[i] == 'S' && i != 0 && str[i-1] == 'S'){
// sum = 0;k = 0;lst = 0;
// }
// if(str[i] == 'S' && !flag && str[i+1] == 'G' && i < N-1){
// flag = true;
// //printf("sum:%d\n", sum);
// lst = sum;
// if(sum <= snt)
// sum++;
// continue;
// }
// else if(str[i] == 'S' && flag == true && str[i-1] != 'S'){
// //printf("len:%d sum:%d\n",len, sum);
// if(sum >= len) return true;
// sum-=lst;
// lst=k;
// k=0;
// }
// else if(str[i] == 'G' && sum <= snt){
// //printf("len:%d i:%d sum:%d\n", len, i , sum);
// sum++;
// k++;
// if(sum >= len) return true;
// }
// }
// //printf("len:%d sum:%d\n", len, sum);
// //if(sum >= len) return true;
// return false;
//}
//
//int main()
//{
// scanf("%d", &N);
// scanf("%s", str);
// snt = 0;
// for(int i = 0; i < N; i++){
// if(str[i] == 'G') snt++;
// }
// //printf("%d\n", snt);
// int l = 0, r = N;
// int mid = 0, ans = 0;
// while(l<=r){
// //printf("mid:%d\n", mid);
// mid = (l+r)>>1;
// if(check(mid)) {
// l = mid+1;
// ans=mid;
// }
// else r = mid-1;
// }
// //printf("l:%d snt:%d\n", l, snt);
// if(ans == snt+1) ans = snt;
// printf("%d\n", ans);
// return 0;
//} #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e5+;
char str[MAXN];
int N;
int main()
{
scanf("%d", &N);
scanf("%s", str);
int gg = ;
int g = , k = ;
int len = ; for(int i = ; i < N; i++){
if(str[i] == 'G'){
g++;
k++;
}
else{
gg = g;
g = ;
}
len = max(len, gg+g+);
}
//printf("%d %d\n", len, k);
int ans = min(len, k);
printf("%d\n", ans);
return ;
}

Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】的更多相关文章

  1. 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将所有奖杯排成一排,你 ...

  2. 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 ...

  3. 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 ...

  4. 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-& ...

  5. 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 ...

  6. 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 ...

  7. Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency

    E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...

  8. Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph

    D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...

  9. Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition

    C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...

随机推荐

  1. SqlServer function 函数

    SqlServer的数据库Tsql还是很强大,以此来纪念下表值函数的语法吧. -- ============================================= -- Author: & ...

  2. Redis - 数据类型常用命令

    5种数据类型都离不开key,先列出key的相关命令. KEY相关操作 列出符合规则的KEYS KEYS pattern pattern支持glob风格的通配符格式,即: ? 一个字符 * 任意多个字符 ...

  3. ssm架构添加maven、shiro、lucene、ueditor、druid支持

    1.pom.xml文件配置: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...

  4. HttpClient 入门教程学习

    HttpClient简介 HttpClient是基于HttpCore的HTTP/1.1兼容的HTTP代理实现. 它还为客户端认证,HTTP状态管理和HTTP连接管理提供可重用组件. HttpCompo ...

  5. Java 基础(7)——运算符

    学完基础的变量常量等知识.再往后和变量常量紧密相关的当然是加减乘除等等运算方法了~(当然加减乘除也只是一部分) 首先按照运算过程参与的元素,把运算符号简单粗暴的分为一元运算符.二元运算符.三元运算符等 ...

  6. JAVA中LinkedLockingQueue的简单使用

    1.相关知识的了解 阻塞队列:当队列为空时,去队列中取数据会被阻塞.当队列满时,往队列中放数据会被阻塞.   非阻塞队列:当队列为空时,去队列取数据会直接返回失败,队列满时,往队列中放数据会直接返回失 ...

  7. SpringCloud实战之初级入门(三)— spring cloud config搭建git配置中心

    目录 1.环境介绍 2.配置中心 2.1 创建工程 2.2 修改配置文件 2.3 在github中加入配置文件 2.3 修改启动文件 3. 访问配置中心 1.环境介绍 上一篇文章中,我们介绍了如何利用 ...

  8. Oracle 数据库字典 sys.col$ 表中关于type#的解释

    sys.col$ 表是oracle基础数据字典表中的列表,表中描述了数据库中各列信息,其中type#是列的数据类型.以下表格说明了各个数值的含义,以供参考. 值 说明 1 如果列 charsetfor ...

  9. Vue-resource和Axios对比以及Vue-axios

    vue更新到2.0之后,作者就宣告不再对vue-resource更新,而是推荐的axios. vue-resource特点 vue-resource插件具有以下特点: 1,体积小 vue-resour ...

  10. git管理之源切换

    Git remote 修改源 git commit -m "Change repo." # 先把所有为保存的修改打包为一个commit git remote remove orig ...