传送门

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. LR 场景选项配置--笔记

    1 tools-options --设置关系到loadgenerator行为应用于一个场景中的所有的load generator 这些设置用于未来所有运行的场景并且通常只需要设置一次 2 expert ...

  2. 在文件保存中 os.getcwd() os.listdir() os.makedirs() os.mkdir() xx.join() ... 等函数 的使用介绍

    path = 'C:\\Users\\zhangjunming\\Desktop\\PycharmProjects\\my_mgm' 1.xx.join(obj)   以xx为分隔符 对obj中的元素 ...

  3. 关于SQL查询语句中的LIKE模糊查询的解释

    LIKE语句的语法格式为: select * from 表名 where 字段名 like 对应值(字符串) 注:主要是针对字符型字段的,它的作用是在一个字符型字段列中检索包含对应字符串的. 下面列举 ...

  4. orcale建表脚本

    declare v_cnt number; V_SQL VARCHAR2 (500) := '';begin select count(*) into v_cnt from dual where ex ...

  5. Windows & RabbitMQ:集群(clustering) & 高可用(HA)

    描述:我们需要配置三台服务器:ServerA, ServerB, ServerC 注意事项: 所有的服务器的Erlang版本,RabbitMQ版本必须一样 服务器名大小写敏感 Step 1:安装Rab ...

  6. ES6函数增强

    函数参数可以拥有默认值.调用函数时,如果没有进行相应的实参传递,参数就会使用默认值.怎么给参数提供默认值呢?很简单,声明函数时候,给形参赋一个值就可以了,这个值就是参数的默认值. // num2拥有默 ...

  7. NoClassDefFoundError与ClassNotFoundException

    原文地址: https://blog.csdn.net/jamesjxin/article/details/46606307 怎么解决NoClassDefFoundError错误 NoClassDef ...

  8. 通过JPA注解获取某个类的主键字段

    public String getPkColumn(String className) { String pkColumn = null; try { Class clazz = Class.forN ...

  9. php 编译常见错误

    1.configure: error: No curses/termcap library found 网上有的说法是:–with-named-curses-libs=/usr/lib/libncur ...

  10. Mining Station on the Sea HDU - 2448(费用流 || 最短路 && hc)

    Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...