传送门: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. [转]AngularJS 实现 Table的一些操作(示例大于实际)

    本文转自:http://www.cnblogs.com/lin-js/p/linJS.html <!DOCTYPE html> <html> <head> < ...

  2. [转]前端构建工具gulpjs的使用介绍及技巧

    本文转自:http://www.cnblogs.com/2050/p/4198792.html gulpjs是一个前端构建工具,与gruntjs相比,gulpjs无需写一大堆繁杂的配置参数,API也非 ...

  3. sql语句将身份证号数字转换成特殊字符

    SELECT Tname , STUFF(Idcard,,,'*********') as Idcard,Completion from demo

  4. ie6的display:inline-block实现

    摘抄自原文链接 简单来说display:inline-block,就是可以让行内元素或块元素变成行内块元素,可以不float就能像块级元素一样设置宽高,又能像行内元素一样轻松居中. 在ie6中给div ...

  5. 本地如何将svn和git管理的代码做关联

    svn和git都是广为流传的代码版本管理工具,实际项目中往往会将两者结合使用,那么如何将本地的一份代码和两者做有机的关联呢! 前提假设:项目已经在开发阶段中,此时变更了svn代码库的地址:或者是组里来 ...

  6. .net EF框架 MySql实现实例

    1.nuget中添加包EF和MySql.Data.Entity 2.config文件添加如下配置 1.配置entitframework节点(一般安装EF时自动添加) <entityFramewo ...

  7. C Primer Plus note6

    error: invalid preprocessing directive #difine| 无效的宏定义处理 宏定义define 写成了 difine.

  8. Cocos2d-js 开发记录:基本图形绘制

    做着做着想要用基本绘图函数画个矩形,在cocos2d-js 3.0里可以使用DrawNode var dn = new cc.DrawNode(); var ltp = cc.p(0, 32); va ...

  9. Spring 框架(三)

    1 spring l AOP :切面编程 切面:切入点 和 通知 结合 l spring aop 编程 <aop:config> 方法1: <aop:pointcut express ...

  10. Android学习——BroadCast(一)

    初识广播 BroadCast即为广播,为安卓四大组件之一,用于在应用程序和Activity间传输信息.一条广播,分为发送和接收两部分,发送方通过Intent存储信息,并进行发送.接收方通过BroadC ...