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 ...
随机推荐
- IOS - UIView停止交互
UIView停止交互(失去焦点): 设置userInteractionEnabled=NO, 获取交互设置YES. 能够应用于UIButton, UITextField等交互型控件. [editBut ...
- 一次c3p0连接池连接异常错误的排查
近期写了一个数据库採集程序,大概过程是将SQLSERVER数据库的数据定时採集到Oracle数据库. 1小时出一次数据,每次数据量在2W左右.环境採用Sping3+hibernate4,数据库连接池採 ...
- linux下误删数据文件恢复
linux下文件被删除能够用非常多工具进行恢复.比如undelete(适合ext2,ext3).giis(不能恢复安装giis之前的文件).ext3grep(仅限ext3).R-linux(支持ext ...
- 速度上手LM4F LaunchPad 输出多路PWM波
最近转战到TI的Cortex M4平台后,发现网上关于TI的LM4F120 Launchpad 资料太少了,而且其中大部分都是TI员工或者其合作伙伴提供的,例程太少,导致新手上手很慢. 我只是要实现几 ...
- Linux ALSA声卡驱动之六:ASoC架构中的Machine
前面一节的内容我们提到,ASoC被分为Machine.Platform和Codec三大部分,其中的Machine驱动负责Platform和Codec之间的耦合以及部分和设备或板子特定的代码,再次引用上 ...
- E20170807-mk
literal adj. 照字面的; 原义的; 逐字的; 平实的,避免夸张;
- ANDROID 开发之 SQLite
SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLit ...
- mysql简单增删改查(CRUD)
先描述一下查看表中所有记录的语句以便查看所做的操作(以下所有语句建议自己敲,不要复制以免出错): user表,字段有 id, name,age,sex:id为主键,自增,插入时可以写 NULL 或者 ...
- Android Eclipse 安装教程 2016.06.13版
2016.8.16修改 第一步,也是最为关键的一步——修改hosts文件 为什么说是最关键的一步呢?因为接下来的操作,我们都需要连接google网,也就是要连接国外的网站.一般情况下,国外的网站是无法 ...
- html——meta标签、link标签
<meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. <meta> 标签位于文档的头部,不包含任何内容.&l ...