传送门: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. deblurGAN

    -- main.py -- util.py -- data_loader.py -- mode.py -- DeblurGAN.py -- vgg19.py -- layer.py -- vgg19. ...

  2. tomcat绑定域名绑定端口及更换ROOT目录

    一.更换ROOT目录 tomcat默认网站目录为 webapps/ROOT ,那么我们如何改为自己的网站目录呢? 1.打开并编辑tomcat目录下的 conf/server.xml 大约在148行的位 ...

  3. Asp.NET MVC4 + Ajax 实现多文件上传

    本文转自http://www.cnblogs.com/freeliver54/archive/2013/05/15/3079700.html JS部分测试可以,jQuery部分没有测试先留着 HTML ...

  4. 懒汉单例安全basedao

    package Dao; import java.sql.*; public class BaseDao { private String drname = "com.mysql.jdbc. ...

  5. IO流之打印流

    打印流的概述(只有输出就是只与数据目的有关,不会抛出IO异常) 打印流添加输出数据的功能,使它们能够方便地打印各种数据值表示形式. 打印流根据流的分类: l  字节打印流  PrintStream l ...

  6. Python入门-内置函数一

    什么是内置函数?就是python给你提供的拿来直接用的函数,比如print,input等等,截止到python版本3.6.2 python一共提供了68个内置函数,他们就是python直接提供给我们的 ...

  7. 合并excel的多个sheet

    '合并excel的多个sheetSub 合并当前工作簿下的所有工作表()Application.ScreenUpdating = FalseFor j = 1 To Sheets.Count If S ...

  8. 各种优化方法总结比较(sgd/momentum/Nesterov/adagrad/adadelta)

    前言 这里讨论的优化问题指的是,给定目标函数f(x),我们需要找到一组参数x,使得f(x)的值最小. 本文以下内容假设读者已经了解机器学习基本知识,和梯度下降的原理. SGD SGD指stochast ...

  9. 如何对MySQL数据库中的数据进行实时同步

  10. Tesseract-OCR-04-使用 jTessBoxEditor 进行训练

    Tesseract-OCR-04-使用 jTessBoxEditor 进行训练 本篇是关于 jTessBoxEditor 进行训练,使 Tesseract-OCR 文字识别准确率得到极大的提高,本篇完 ...