cf Educational Codeforces Round 55 B. Vova and Trophies

原题:
B. Vova and Trophies
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vova has won 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.

Input
The first line contains one integer n (2≤n≤10^5) — the number of trophies.

The second line contains n characters, each of them is either G or S. If the i-th character is G, then the i-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
input
10
GGGSGGGSGG
output
7
inputCopy
4
GGGG
output
4
input
3
SSS
output
0
Note
In the first example Vova has to swap trophies with indices 4 and 10. Thus he will obtain the sequence “GGGGGGGSGS”, the length of the longest subsegment of golden trophies is 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.

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.

中文:

有金奖杯G和银奖杯S,现在给你n个奖杯和排放顺序,现在让你任意交换这些奖杯中的其中两个位置,只交换一次,问你金奖杯最长能摆多少个?

代码:

#include<bits/stdc++.h>
using namespace std;

int n;
string s;
typedef pair<int,int> pii;
vector<pii> vp;
map<int,int> start,en;

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n)
    {
        vp.clear();
        start.clear();
        en.clear();
        cin>>s;
        int st=0;
        int G=0,ans=0;
        for(int i=0;i<n;)
        {
            if(s[i]=='G')
            {
                st=i;
                while(s[i]=='G')
                {
                    G++;
                    i++;
                }
                vp.push_back(make_pair(st,i-1));
                ans=max(ans,i-st);
                start[st]=i-1;
                en[i-1]=st;
            }
            else
                i++;
        }
        if(vp.size()>1)
            ans=ans+1;
        int cnt,tmp;
        for(auto x:vp)
        {
            cnt=x.second-x.first+1;
            if(en.find(x.first-2)!=en.end())
            {
                tmp=x.first-1-en[x.first-2];
                if(tmp+cnt<G)
                    ans=max(ans,tmp+cnt+1);
                else
                    ans=max(ans,tmp+cnt);
            }
            if(start.find(x.second+2)!=start.end())
            {
                tmp=start[x.second+2]-x.second-1;
                if(tmp+cnt<G)
                    ans=max(ans,tmp+cnt+1);
                else
                    ans=max(ans,tmp+cnt);
            }
        }
        cout<<ans<<endl;
    }

    return 0;
}

思路:

首先把所有金奖杯的序列都单独拿出来,如果金奖杯的序列只有一个,那么答案就是这个序列的长度。
如果金奖杯的序列有多个,那么要分几种情况。

第一种,两个金奖杯序列之间隔了一个银奖杯,那么可以把其中一个序列末尾的金奖杯序列和这个银奖杯调换位置,这样就连成了一个序列,找出所有这种情况中最长的即可。

第二种,如果两个金奖杯序列之间隔着超过一个银奖杯,那么就是最长的金奖杯序列后面或者前面的银奖杯和另外一个金奖杯序列中的一个奖杯交换位置,得到的结果就是最长的那个金奖杯序列的长度加一。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值