Codeforces 1082 B. Vova and Trophies-有坑 (Educational Codeforces Round 55 (Rated for Div. 2))
2 seconds
256 megabytes
standard input
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.
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.
Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.
10
GGGSGGGSGG
7
4
GGGG
4
3
SSS
0
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的长度交换一个S为G的比较一下,wa在这里了。
代码:
//B
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int inf=0x3f3f3f3f; char c[maxn];
vector<int> ve; int main()
{
int n;cin>>n;
cin>>c;
int num=;
for(int i=;i<n;i++){
if(c[i]=='G') num++;
if(c[i-]=='G'&&c[i]=='S'&&c[i+]=='G') ve.push_back(i);
}
int ret=,tmp=;
for(int i=;i<n;i++){
if(c[i]=='G') tmp++;
else ret=max(ret,tmp),tmp=;
}
ret=max(ret,tmp);
ret=min(ret+,num);
if(num==n) {cout<<n<<endl;return ;}
if(num==) {cout<<<<endl;return ;}
if(ve.size()==){
int ans=-,cnt=;
for(int i=;i<n;i++){
if(c[i]=='G') cnt++;
if(c[i]=='S') ans=max(ans,cnt),cnt=;
}
ans=max(ans,cnt);
cout<<min(num,ans+)<<endl;
}
else{
int ans=,flag=;
for(auto it:ve){
int cnt=;
for(int i=it-;i>=;i--){
if(c[i]=='S') break;
else cnt++;
}
for(int i=it+;i<n;i++){
if(c[i]=='S') break;
else cnt++;
}
ans=max(ans,cnt);
}
ans=min(num,ans+);
cout<<max(ans,ret)<<endl;
}
} /*
16
GSGSSGSSGGGSSSGS 4
*/
Codeforces 1082 B. Vova and Trophies-有坑 (Educational Codeforces Round 55 (Rated for Div. 2))的更多相关文章
- 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 (贪心+字符串)
B. Vova and Trophies time limit per test2 seconds memory limit per test256 megabytes inputstandard i ...
- Educational Codeforces Round 55 (Rated for Div. 2) Solution
A. Vasya and Book Solved. 三种方式取$Min$ #include <bits/stdc++.h> using namespace std; #define ll ...
- 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将所有奖杯排成一排,你 ...
- 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 D. Maximum Diameter Graph-树的直径-最长链-构造题 (Educational Codeforces Round 55 (Rated for Div. 2))
D. Maximum Diameter Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 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) A/B/C/D
http://codeforces.com/contest/1082/problem/A WA数发,因为默认为x<y = = 分情况讨论,直达 or x->1->y or x-& ...
- 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 ...
随机推荐
- UVA 10886 Standard Deviation
https://vjudge.net/problem/UVA-10886 计算标准差 碰到这种题将式子展开 #include<cmath> #include<cstdio> / ...
- hdu 3948 The Number of Palindromes
The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (J ...
- 搜索:BFS
如果题目真的要考察宽度优先搜索,那么这类题目往往具有比较大的编码难度,换个说法,就是细枝末节特别多,状态特别复杂.. 剥茧抽丝,这里以一个比较“裸”的BFS作为例子,了解一下实现BFS的一些规范. 直 ...
- django中处理表单的经典流程
def form_process_view(request): if request.method == 'POST': # 请求为 POST,利用用户提交的数据构造一个绑定了数据的表单 form = ...
- PowerDesigner逆向工程
再用PD建表完成后导成SQL脚本然后在SQL Server中运行后生成数据库后,就想到,可不可以将直接将数据库的内容生成PD文档?经过上网查,当然可以的. 要将SQL Server中的数据库导入到PD ...
- Linux SSH 无密码登录
1. ssh-keygen -t rsa 2. scp root@ip:/root/.ssh/id_rsa.pub ./id2 3. cat id2 >> authtorized_keys ...
- [BZOJ1076][SCOI2008]奖励关解题报告|状压DP
你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再吃). 宝 ...
- Java 中的成员内部类
内部类中最常见的就是成员内部类,也称为普通内部类.我们来看如下代码: 运行结果为: 从上面的代码中我们可以看到,成员内部类的使用方法: 1. Inner 类定义在 Outer 类的内部,相当于 Out ...
- LCD实验学习笔记(五):MMU
内存管理分别页表机制和内存分配机制两块. 页表机制就是管理设备真实物理地址与虚拟地址的动态或静态的映射,基于cpu内部的mmu(内存管理单元)进行. CP15(协处理器)的C0(缓存)是一级页表,含4 ...
- Linux 入门记录:九、Linux 文件系统挂载管理
一.挂载操作 磁盘或分区创建好文件系统后,需要挂载到一个目录才能够使用. Windows 或 Mac 系统会进行自动挂载,一旦创建好文件系统后会自动挂载到系统上,Windows 上称之为 C 盘.D ...