【CF1082B】 Vova and Trophies

题目

题意翻译
题目描述
你现在有 nn 枚奖牌,每枚奖牌为金牌或银牌。这些奖牌现在按顺序排成一排。现在你可以调换任意一对奖牌的位置,求金牌最长连续段。

输入格式
第一行一个整数 nn (2\le n\le 10^5)(2≤n≤10
5
) .

第二行一行字符串,表示你的奖牌。 ( \texttt{G}G 表示金牌,\texttt{S}S 表示银牌)

输出格式
输出一个整数表示答案。

题目描述
Vova has won n n 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 n n ( 2 \le n \le 10^5 2≤n≤10
5
) — the number of trophies.

The second line contains n n characters, each of them is either G or S. If the i i -th character is G, then the i i -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.

输入输出样例
输入样例#1: 复制
10
GGGSGGGSGG
输出样例#1: 复制
7
输入样例#2: 复制
4
GGGG
输出样例#2: 复制
4
输入样例#3: 复制
3
SSS
输出样例#3: 复制
0
说明
In the first example Vova has to swap trophies with indices 4 4 and 10 10 . Thus he will obtain the sequence “GGGGGGGSGS”, the length of the longest subsegment of golden trophies is 7 7 .

In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is 4 4 .

In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than 0 0 .

思路

先预处理出来每一位的连续G个数,然后再预处理出在开头或结尾是否有跟这个不连续但是仍然是G.

代码

#include<bits/stdc++.h>
using namespace std;
int n,maxn;
char s[100003];
int a[100003],flag;
bool b[100003];
int main(){
    cin>>n;
    cin>>s+1;
    for(int i=1;i<=n;i++){
        if(s[i]=='G')a[i]=a[i-1]+1,flag=1;
        //123012301234012
        maxn=max(maxn,a[i]);
    }
    if(flag==0){cout<<"0";return 0;}
    for(int i=n-1;i>=1;i--){
        if(a[i]&&a[i+1])
           a[i]=a[i+1];

    }
    int f=0;
    for(int i=1;i<=n;i++){
        if(a[i])f=1;
        else if(f==1){
            for(int j=i+1;j<=n;j++)
               b[j]=true;
            break;
        }
    }
    f=0;
    for(int i=n;i>=1;i--){
        if(a[i])f=1;
        else if(f==1){
            for(int j=1;j<=i-1;j++)
               b[j]=true;
            break;
        }
    }
   // for(int i=1;i<=n;i++)
       //cout<<a[i]<<" ";
    for(int i=1;i<=n;i++){
        if(a[i])continue;

        if(b[i])maxn=max(maxn,a[i-1]+a[i+1]+1);
        maxn=max(maxn,a[i-1]+a[i+1]);
    }
    cout<<maxn;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值