Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies
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());
}
在trophy的最前和最后各补一个'S',每次查找相邻的三个'S',判断是否可以通过将第二个'S'换成‘G'使连续的金奖杯个数最大。
Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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-& ...
- 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 ...
- 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 ...
- Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency
E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...
- Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph
D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...
- Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition
C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...
随机推荐
- ConnectTimeout和ReadTimeout所代表的意义
参考:ConnectTimeout和ReadTimeout所代表的意义 ConnectTimeout 指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间. 在java中,网络状况 ...
- solr部署tomcat 访问HTTP Status 403 – Access to the requested resource has been denied
-----------解决403错误看这里!!-----------打开****\Tomcat 8.5\webapps\solr\WEB-INF里面的web.xml,把下面这段配置注释掉!!!如下所示 ...
- Jenkins+PowerShell持续集成环境搭建(四)常用PowerShell命令
0. 修改执行策略 Jenkins执行PowerShell脚本,需要修改其执行策略.以管理员身份运行PowerShell,执行以下脚本: Set-ExecutionPolicy Unrestricte ...
- Previous Workflow Versions in Nintex Workflow
Previous Workflow Versions in Nintex Workflow September 4, 2013 It occurred to me that even though I ...
- git 命令使用集锦
使用git mv重命名文件,而不是delete然后再add文件. git format常用命令: git format-patch -4 //从当前分支最新提交点往下共生成4个补丁 git forma ...
- BZOJ5203 [NEERC2017 Northern] Grand Test 【dfs树】【构造】
题目分析: 首先观察可知这是一个无向图,那么我们构建出它的dfs树.由于无向图的性质我们可以知道它的dfs树只有返祖边.考虑下面这样一个结论. 结论:若一个点的子树中(包含自己)有两个点有到它祖先的返 ...
- CH2906 武士风度的牛(算竞进阶习题)
水..... 直接bfs... #include <bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; typedef l ...
- ios-deploy was not found
Ionic 打包ios的时候,突然报错,提示如下: (node:1157) UnhandledPromiseRejectionWarning: ios-deploy was not found. Pl ...
- mysql 0x80004005 unable to connect to any of the specified mysql hosts
语言:c# 问题:偶尔会出现连不上mysql 报标题的这个错误. 解决方法:把server = localhost 改为 =127.0.0.1 或者静态IP ,按着改暂时没出现了,继续观望!
- 【支付宝】"验签出错,sign值与sign_type参数指定的签名类型不一致:sign_type参数值为RSA,您实际用的签名类型可能是RSA2"
问题定位:从描述就可以看的出来了,你现在sign_type是 RSA类型的,要改成跟你现在用的签名类型一致的类型,也就是 要改为 RSA2 PHP为例 // 新版只支持此种签名方式 商户生成签名字符 ...