传送门

https://www.cnblogs.com/violet-acmer/p/10035971.html

题意:

  Vova有n个奖杯,这n个奖杯全部是金奖或银奖,Vova将所有奖杯排成一排,你最多可以交换其中两个奖杯,求最大的连续的金奖杯个数。

题解:

  思路:

    求出连续的金奖杯位置,找出每两个相邻的连续的金奖杯所能够形成的最大的连续的金奖杯的个数,输出最大值。

  相关变量解释:

 int n;
char trophy[maxn];
struct Node
{
int l,r;//连续金奖杯包含的区间[l,r)
Node(int _a=,int _b=):l(_a),r(_b){}
}gold[maxn];//记录连续金奖杯的位置信息

  步骤:

  (1):遍历一遍数组,记录出连续金奖杯的左右区间;

  (2):每两个连续区间求最大连续的金奖杯个数

具体看代码:

 #include<iostream>
#include<cstdio>
using namespace std;
const int maxn=1e5+; int n;
char trophy[maxn];
struct Node
{
int l,r;//连续金牌包含的区间[l,r)
Node(int _a=,int _b=):l(_a),r(_b){}
}gold[maxn];//记录连续金牌的位置信息 int Solve()
{
int index=;
int cnt=;
gold[]=Node(,);//初始化gold[1],防止没有'G'的情况
while(index < n)
{
while(index < n && trophy[index] == 'S')
index++;
int l=index;
while(index < n && trophy[index] == 'G')
index++;
int r=index;
if(l != r)
gold[++cnt]=Node(l,r);
}
int res=gold[].r-gold[].l;//res初始化为gold[1]的金奖杯长度,防止只有一个连续的金奖杯
for(int i=;i <= cnt;++i)//从第二个开始,每次查找i和i-1可以形成的最大的连续的金奖杯个数
{
int preG=gold[i-].r-gold[i-].l;
int backG=gold[i].r-gold[i].l; if(gold[i].l-gold[i-].r == )//如果两个连续的金奖杯间只有一个银奖杯
res=max(res,preG+backG+(cnt > ? :));
else
res=max(res,max(preG,backG)+);
}
return res;
} int main()
{
scanf("%d",&n);
scanf("%s",trophy);
printf("%d\n",Solve());
}

  xiaokai的思路

  在trophy的最前和最后各补一个'S',每次查找相邻的三个'S',判断是否可以通过将第二个'S'换成‘G'使连续的金奖杯个数最大。

Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies的更多相关文章

  1. 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 ...

  2. Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies (贪心+字符串)

    B. Vova and Trophies time limit per test2 seconds memory limit per test256 megabytes inputstandard i ...

  3. Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】

    传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limit per test 2 ...

  4. Educational Codeforces Round 55 (Rated for Div. 2) A/B/C/D

    http://codeforces.com/contest/1082/problem/A WA数发,因为默认为x<y = = 分情况讨论,直达 or x->1->y  or  x-& ...

  5. Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))

    C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input ...

  6. Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

    A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency

    E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...

  8. Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph

    D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...

  9. Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition

    C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...

随机推荐

  1. 莫烦theano学习自修第一天【常量和矩阵的运算】

    1. 代码实现如下: #!/usr/bin/env python #! _*_ coding:UTF-8 _*_ # 导入numpy模块,因为numpy是常用的计算模块 import numpy as ...

  2. 老男孩python学习自修第十九天【异常处理】

    1.常见的错误 TypeError 类型错误 NameError 没有该变量 ValueError 不期望的值 AttributeError 没有该属性 UnboundLocalError 没有该局部 ...

  3. vue ajax

    局部get: this.$http.get(url,{param:jsonData}).then(successCallback,failCallBack) 局部post: this.$http.po ...

  4. js模拟ctrl+c的问题

    1.这种方式只可以对显示的textbox和textarea使用,对于display:none和visibility hidden 以及其他标签无效 var message = document.get ...

  5. 给dom对象添加事件

  6. Nginx 减少关闭连接的time_wait端口数量

    L:129

  7. MD5进行解密操作

    package com.dyy.test; import java.security.MessageDigest; public class TestMD5Util { /*** * MD5加码 生成 ...

  8. VM虚拟机截图方法介绍

    可以先安装QQ之类的截图软件,但比较麻烦,而且截图之后还需要安装VMware Tools等工具才能拿到物理机上 先定向到物理机,快捷键为CTRL+ALT,之后在用qq截图快捷键ctrl+alt+a即可 ...

  9. Django+Xadmin打造在线教育系统(四)

    完成授课机构的功能 模板继承 在templates目录下,新建base.html,剪切org-list.html内容到里面 编写org-list.html内容 继承base.html,将里面的面包屑和 ...

  10. synchronized 关键字解析

    synchronized 关键字解析 同步锁依赖于对象,每个对象都有一个同步锁. 现有一成员变量 Test,当线程 A 调用 Test 的 synchronized 方法,线程 A 获得 Test 的 ...