• 给大佬的奖杯排序(CodeForces


    题目链接:http://codeforces.com/problemset/problem/1082/B

    B. Vova and Trophies
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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.

    Input

    The first line contains one integer nn (2n1052≤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

    input
    Copy
    10
    GGGSGGGSGG
    
    output
    Copy
    7
    
    input
    Copy
    4
    GGGG
    
    output
    Copy
    4
    
    input
    Copy
    3
    SSS
    
    output
    Copy
    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.

    大佬的奖杯真是多,而且大佬想要把尽量多的金杯放在一起(手动滑稽).不过他只想最多交换一次。这道题的要求就是让你写个代码把尽量多的的奖杯放在一起

    但是你只能交换一次或者不交换。

    题目的大体思维:用两个数保存两个连续区段奖杯的数量。不过只有当两个连续区段相差为1的时候才能够把两个区段加起来+1,否则只能自加1.

    但是这道题有特殊情况,比如当奖杯排序为GGGGSGGGG的时候,这个时候并不能去交换再去+1,因此只能用另一个数来保存连续奖杯的数量

    此后再取一下ans和统计gold奖杯的小值就可以了

    附上丑陋的AC代码

     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 string a;
     5 int main()
     6 {
     7     int n,ans=0,l=0,sumg=0,r=0;
     8     cin>>n;
     9     cin>>a;
    10     for(int i=0;i<n;i++)
    11     {
    12         if(a[i]=='G')
    13         {
    14             sumg++;
    15            r++;
    16         }
    17         else
    18         {
    19             l=r;
    20            r=0;
    21         }
    22         ans=max(ans,r+l+1);
    23     }
    24     ans=min(ans,sumg);
    25     printf("%d",ans);
    26     return 0;
    27 }
    View Code

    人生第二篇博客。。。。。。。(参考其他人的,因为比赛的时候没有AC这道题)

  • 相关阅读:
    Java基础面试题总结-编程题总结
    Linux下mysql的安装与卸载并且连接navicat详解(亲测可用)
    linux 下安装redis
    linux 下安装tomcat
    Linux系统下安装jdk及环境配置(两种方法)
    Servlet概述
    多线程之volatile关键字
    多线程之ThreadLocal
    多线程之synchronized实现原理
    线程池2
  • 原文地址:https://www.cnblogs.com/tombraider-shadow/p/10294730.html
Copyright © 2020-2023  润新知