Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】
传送门:http://codeforces.com/contest/1082/problem/B
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' 序列的长度。
解题思路:
想得太多系列,一开始用了二分搜索,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 【贪心 】的更多相关文章
- 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将所有奖杯排成一排,你 ...
- 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) C. Multi-Subject Competition 【vector 预处理优化】
传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limit per test 2 ...
- 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-& ...
- 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 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):E. Increasing Frequency
E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...
- Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph
D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...
- Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition
C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...
随机推荐
- 转发与重定向的区别(forward与redirect的区别)
转发:服务器接收到客户端的请求后,在服务器内部传递的过程.最后回复结果给客户端. 重定向:服务器接收到客户端的请求后,回复一个新url给客户端,客户端跳转新url.
- Expression Blend实例中文教程(1) - 开篇
随着计算机软件开发分工细节化,微软对已有的产品线进行了调整,在保持原有经典开发工具Visual Studio基础上,又推出了一套新的设计开发工具系列,Expression Studio. Expres ...
- 【转】XDocument简单入门
1.什么是XML? 2.XDocument和XmlDocument的区别? 3.XDocument 4.XmlDocument 5.LINQ to XML 6.XML序列化与反序列化 因为这几天用到了 ...
- JAVA学习之路(多线程)---模拟售票(细解)
首先看题目描述: 假设有火车票100张,创建4个线程模拟4个售票点,每100ms售出一张,打印出售票过程,格式如下: 窗口3:卖出第100张票 窗口4:卖出第99张票 ............ ... ...
- 最近使用日期控件时,用到了My97DatePicker控件,单日期控件,记录一下
以上是使用时的效果,可以自己设定日期有效区间,如下图: 对于起始日期和终止日期的控制如下: <td> <label >起始日期:</label> <input ...
- canal —— 阿里巴巴mysql数据库binlog的增量订阅&消费组件
阿里巴巴mysql数据库binlog的增量订阅&消费组件canal ,转载自 https://github.com/alibaba/canal 最新更新 canal QQ讨论群已经建立,群号 ...
- Node.js开发——MongoDB与Mongoose
为了保存网站的用户数据和业务数据,通常需要一个数据库.MongoDB和Node.js特别般配,因为MongoDB是基于文档的非关系型数据库,文档是按BSON(JSON的轻量化二进制格式)存储的,增删改 ...
- mockjs
首先还是那句话,进来的GodBoy and GoodGirl 不妨看完再离开. 一个走在路上的前端攻城狮-along 一.mock的由来 mock有“愚弄.欺骗”之意,在前端领域,mock可以理解为 ...
- css 样式表集合
说到前端不得不说一下css样式 css样式是用来装饰我们的html让整个页面显得更丰富多彩,所以我们要熟悉各种css样式,本人搜集了一下 供大家参考一下 字体属性:(font) 大小 {font-si ...
- HTML5拨号 调用手机拨号功能
<a href="tel:123456789">拨号</a> 这个就是HTML5 运行在手机浏览器上的可以调用手机的拨号tel就是你想要拨打的电话号码