Vova and Trophies CodeForces - 1082B(思维题)
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
10
GGGSGGGSGG
7
4
GGGG
4
3
SSS
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.
当时做题的时候交了一发过了,不过最后竟然被hack了,哎,还是太弱了,要自闭了。嘤嘤嘤;
题意很简单,就不说了,
思路:
1、我的思路:找一下连续的G串有多少串sumg并记录最大值maxx1,然后再找 如果一个S的左边和右边都是G串那么再记录一下此类串的最大长度maxx2
如果sumg==1输出maxx1
如果sumg==2输出max(maxx1+1,maxx2);
如果sumg>2输出max(maxx1+1,maxx2+1);
2、大佬的思路:
记录中间 ‘S’ 前面的 ‘G’的长度 和 后面 'G' 的长度,作和。取最大值 maxlen。最后答案和 总的‘G’数量 snt 进行一下比较,如果大了说明多加的,输出 snt,否则输出 maxlen
以下是鄙人的代码,很繁琐;
下面还有大佬的代码;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int INF=0x3f3f3f3f;
char a[maxn];
int main()
{
int n,sumg=, tempg=;
scanf("%d",&n);
getchar();
scanf("%s",a);
int maxx1=,maxx2=,temps=,flag=,CNT=;
for(int i=;i<n;i++)
{
if(a[i]=='G')
{
sumg++;
tempg++;
if(i==n-)
{
if(temps)
maxx1=max(maxx1,temps+tempg);
else
maxx2=max(maxx2,tempg);
CNT++;
}
}
else
{
if(tempg)CNT++;
if(flag==)
{
maxx1=max(tempg+temps,maxx1);
temps=tempg;
tempg=;
flag=;
}
if(a[i+]!='G')
{ maxx2=max(maxx2,tempg);
tempg=;
temps=;
}
else
{
if(tempg&&a[i-]=='G')
flag=;
temps+=tempg;
tempg=;
}
}
}
if(sumg==n)
printf("%d\n",n);
else if(sumg)
{
if(CNT==)
{
printf("%d\n",max(maxx1,maxx2+));
}
else if(CNT==)
{
printf("%d\n",maxx2);
}
else
{
printf("%d\n",max(maxx1+,maxx2+));
}
}
else
printf("0\n");
return ;
}
一下是大佬的代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int INF=0x3f3f3f3f;
string a;
int main()
{
int n,ans=,temps=,sumg=,tempg=;
cin>>n;
cin>>a;
for(int i=;i<n;i++)
{
if(a[i]=='G')
{
sumg++;
tempg++;
}
else
{
temps=tempg;
tempg=;
}
ans=max(ans,tempg+temps+);
}
ans=min(ans,sumg);
printf("%d",ans);
return ;
}
Vova and Trophies CodeForces - 1082B(思维题)的更多相关文章
- Codeforces 424A (思维题)
Squats Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- CodeForces - 417B (思维题)
Crash Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
- CodeForces - 417A(思维题)
Elimination Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
- B. Vova and Trophies 字符串预处理+思维+贪心
题意:给出一个字符串 只有G和S 可以交换任意两个位置的字符一次 问 最长的G的长度是多少 思路:预处理字符串 把相同的G粘成一个G 记一下数量 字符串变为 GSSGSGGSGSSG 相邻有一个S ...
- B - Sonya and Exhibition CodeForces - 1004B (思维题)
B. Sonya and Exhibition time limit per test 1 second memory limit per test 256 megabytes input stand ...
- CF1082B Vova and Trophies 题解
CF1082B Vova and Trophies 题解 瞎搞题,推荐的,一看是道水题,就随手A了-- 题目描述 Vova has won \(n\)trophies in different com ...
- Codeforces 1082B Vova and Trophies 模拟,水题,坑 B
Codeforces 1082B Vova and Trophies https://vjudge.net/problem/CodeForces-1082B 题目: Vova has won nn t ...
- Codeforces 1082B Vova and Trophies(前缀+后缀)
题目链接:Vova and Trophies 题意:给定长度为n的字符串s,字符串中只有G和S,只允许最多一次操作:任意位置的两个字符互换.求连续G的最长长度. 题解:维护pre和pr,nxt和nx. ...
- 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 ...
随机推荐
- Ubuntu下安装C/C++开发环境【!!!有更新!!!Ubuntu14.10下使用eclipse搭建C语言开发环境】
(1)第一步安装Eclipse,有两种方法,使用软件市场搜索就可以得到,安装就可以 另外一种是使用终端安装,命令例如以下: sudo su进入root模式 输入password 然后 输入:sudo ...
- JAVA学习第二十一课(多线程(一)) - (初步了解)
放假在家,歇了好几天了,也没学习,今天学习一下多线程.找找感觉.后天就要回学校了.sad... PS:包 没有什么技术含量,会用就可以,日后开发就必需要会用啦,所以打算先放一放,先来多线程 一.多线程 ...
- 解析java中volatilekeyword
在java多线程编程中常常volatile,有时候这个keyword和synchronized 或者lock常常有人混淆.详细解析例如以下: 在多线程的环境中会存在成员变量可见性问题: java的每一 ...
- sed 之 N D P
sed的N,D,P 是用于多行模式空间的命令,分别对应于n,d,p n & N: n(next)输出模式空间的内容,然后读取新的输入行,n命令不创建多行模式空间:N(Next)通过读取新的输入 ...
- git reset --hard 回滚以后 以后怎么再回去?
恢复的过程很简单: 通过git log -g命令来找到需要恢复的信息对应的commitid,可以通过提交的时间和日期来辨别,找到执行reset --hard之前的那个commit对应的commitid ...
- jeesite自定义主题
jeesite cms首页太丑不够逼格,然而国内有很多高大上的皮肤供你选择,那么本文就一步一步教你如何定制自己的CMS站点视图. 1.下载 jeesite 源码,并安装配置成功 2.进入jeesite ...
- jsp jquery js 的基本路径获取
引子:js中需要当前页面的基础路径,获取不到request,可以通过如下方法来解决! 1.jsp基础路径,在jsp头部加上,获取基础路径http://localhost:8080/project/ ...
- poj1006生理周期(中国剩余定理)
生理周期 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 139224 Accepted: 44687 Descripti ...
- Vue 页面回退参数被当作字符串处理
当时情景是这样的,我从A页面跳到B页面时会传一个Boolean类型的参数,当B跳到C,再从C返回B的时候,控制台打印发现参数还在,可是判断怎么都不起作用,后来发现,当页面返回的时候,默认将参数变成了字 ...
- 巴什博弈------最少取件数 不是1的情况下 hdu---2897
最少取件数 是1的时候 核心代码是 // 共有 n 见 物品 一次最少取 一个 最多取 m 个 )==) printf("先取者输"); 在代码中 可以看到 题目中 一共 ...