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个信息,每个信息包含专业编 ...
随机推荐
- SpringMVC 重定向到其他系统的页面的两种方式
//测试重定向到另外的一个系统 @RequestMapping("/tttt") public void testRed(HttpServletResponse response) ...
- How to RAMDISK on macOS
diskutil erasevolume HFS+ 'RAM Disk' `hdiutil attach -nomount ram://8388608`
- Aizu2130-Billion Million Thousand-dp
用dp求出最大的表达,再用dp求出.//然而并没有想出来 #include <cstdio> #include <string> #include <algorithm& ...
- 【BZOJ3379】【USACO2004】交作业 区间DP
题目描述 数轴上有\(n\)个点,你要从位置\(0\)去位置\(B\),你每秒钟可以移动\(1\)单位.还有\(m\)个限制,每个限制\((x,y)\)表示你要在第\(t\)秒之后(可以是第\(t\) ...
- Codeforces Round #530 (Div. 2) A,B,C,D
A. Snowball 链接:http://codeforces.com/contest/1099/problem/A 思路:模拟 代码: #include<bits/stdc++.h> ...
- 【HDU5950】Recursive sequence(矩阵快速幂)
BUPT2017 wintertraining(15) #6F 题意 \(f(1)=a,f(2)=b,f(i)=2*(f(i-2)+f(i-1)+i^4)\) 给定n,a,b ,\(N,a,b < ...
- AES解密后多了\0
AES加密解密之后发现多了几个空格,不知道原因.在调试时发现多了\0这种东西 不知道为什么会多这些.后来.replace("\0","")这样做了事
- Nginx优化文件编写
server_tokens off; #并不会让nginx执行的速度更快,关闭它可隐藏错误页面中的nginx版本号charset utf-8,gbk; #字符#sendfile on;#tcp_nop ...
- Visible Trees HDU - 2841(容斥)
对于已经满足条件的(x1,y1),不满足条件的点就是(n*x1,n*y1),所以要求的就是满足点(x,y)的x,y互质,也就是gcd(x,y) == 1,然后就可以用之前多校的方法来做了 另f[i] ...
- hdu 2328 Corporate Identity(kmp)
Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...