传送门

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. macOS & USB stick

    macOS & USB stick why macOS can only read USB stick, can not write files to USB stick macos 无法写文 ...

  2. linux 查看路由表

    随便转载,保留出处:http://www.cnblogs.com/aaron-agu/ route –n

  3. Lodop打印设计、维护、预览、直接打印简单介绍

    四者的区别和联系:(其中PRINT_DESIGN打印设计是提供给开发人员的,另外三个可开放给用户)PRINT_DESIGN打印设计:辅助开发人员设计,图形化拖动插入修改等,设计完成后,生成代码拷贝到程 ...

  4. Android SDK Mirror

    Android SDK Manager - Tools - Option - Proxy Settings - HTTP Proxy Server mirrors.zzu.edu.cn Force H ...

  5. draknet网络配置参数

    https://blog.csdn.net/hrsstudy/article/details/65447947?utm_source=itdadao&utm_medium=referral [ ...

  6. Deploy .NET Core with Docker

    Creating a .NET Core project If you already have an existing .NET Core project you are more than wel ...

  7. [离散时间信号处理学习笔记] 10. z变换与LTI系统

    我们前面讨论了z变换,其实也是为了利用z变换分析LTI系统. 利用z变换得到LTI系统的单位脉冲响应 对于用差分方程描述的LTI系统而言,z变换将十分有用.有如下形式的差分方程: $\displays ...

  8. 搭建Hexo博客(三)—换电脑继续写Hexo博客

    Hexo和GitHub搭建博客的原理是:Hexo将source下的md文件生成静态的html页面,存放到public目录中,这一步是由命令:hexo -g完成.接下来执行hexo -d命令,就将pub ...

  9. BZOJ3812 主旋律(状压dp+容斥原理)

    设f[S]为S点集是SCC的方案数.考虑通过去掉不合法方案转移.可以枚举入度为0的SCC所含点集S',这样显然S^S'内部的边和由S'连向S^S'的边删还是不删任选.但是这样无法保证S'包含所有入度为 ...

  10. 一个服务器创建两个MySQL

    转载:http://www.2cto.com/database/201412/357863.html 将已安装的数据库文件夹复制到另一个目录下 打开复制目录下的my.ini文件修改 [client] ...