传送门: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. WAMP环境配置-Apache服务器的安装

    一.下载 下载地址:http://httpd.apache.org/ 在这里就可以下载想下载的版本了 二.安装 我这次环境配置安装的是Apache-2.4.23版本! (最近我在反复安装PHP的时候出 ...

  2. EV3DVue干涉检测的优势分析

    过去几年中国制造行业获得了的快速发展,各企业为了尽可能早的抢占市场,对模具的生产周期要求越来越短,精度要求越来越高,这就对模具设计以及制造等各个环节提出了更高的要求.随着CAD/CAM技术的深入应用, ...

  3. php发送邮件功能(PHPMailer-master插件)

    当作一个插件使用即可,放到网站根目录,然后调用里面的mail.php 源码

  4. JDK自带工具keytool生成ssl证书 和 HTTPS双向认证

    创建证书(第一步) keytool -genkey -alias "baidu" -keypass "123456" -keystore "D:/ba ...

  5. 不同浏览器下word-wrap,word-break,white-space强制换行和不换行总结

    强制换行与强制不换行用到的属性 我们一般控制换行所用到的CSS属性一共有三个:word-wrap; word-break; white-space.这三个属性可以说是专为了文字断行而创造出来的.首先我 ...

  6. Python-常用模块1

    今天我们来看一看python中的常用的模块,内容有点多,我会分两天来更新这些知识 一.什么是模块 模块就是我们把装有特定功能的代码就行归类的结果,从代码编写的单位来看我们的程序,从小到大的顺序:一条代 ...

  7. Scrapy安装教程 pip 或 conda 两种安装方法.

      cmd: pip -V    查看pip版本 pip install --upgrade pip        升级最高版本 https://sourceforge.net/projects/py ...

  8. VC学习笔记----STL库

      STL = Standard Template Library,标准模板库,惠普实验室开发的一系列软件的统称.它是由Alexander Stepanov.Meng Lee和David R Muss ...

  9. Java基础之Map的遍历

    遍历Map集合,有四种方法:   public static void main(String[] args) { Map<String, String> map = new HashMa ...

  10. python 测试:wraps

    任务: 现有两个函数: def print1(): print("I am print1") def print2(): print("I am print2" ...