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将所有奖杯排成一排,你最多可以交换其中两个奖杯,求最大的连续的金奖杯个数。

题解:

  思路:

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

  相关变量解释:

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

  步骤:

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

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

具体看代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 const int maxn=1e5+10;
 5
 6 int n;
 7 char trophy[maxn];
 8 struct Node
 9 {
10     int l,r;//连续金牌包含的区间[l,r)
11     Node(int _a=0,int _b=0):l(_a),r(_b){}
12 }gold[maxn];//记录连续金牌的位置信息
13
14 int Solve()
15 {
16     int index=0;
17     int cnt=0;
18     gold[1]=Node(0,0);//初始化gold[1],防止没有‘G‘的情况
19     while(index < n)
20     {
21         while(index < n && trophy[index] == ‘S‘)
22             index++;
23         int l=index;
24         while(index < n && trophy[index] == ‘G‘)
25             index++;
26         int r=index;
27         if(l != r)
28             gold[++cnt]=Node(l,r);
29     }
30     int res=gold[1].r-gold[1].l;//res初始化为gold[1]的金奖杯长度,防止只有一个连续的金奖杯
31     for(int i=2;i <= cnt;++i)//从第二个开始,每次查找i和i-1可以形成的最大的连续的金奖杯个数
32     {
33         int preG=gold[i-1].r-gold[i-1].l;
34         int backG=gold[i].r-gold[i].l;
35
36         if(gold[i].l-gold[i-1].r == 1)//如果两个连续的金奖杯间只有一个银奖杯
37             res=max(res,preG+backG+(cnt > 2 ? 1:0));
38         else
39             res=max(res,max(preG,backG)+1);
40     }
41     return res;
42 }
43
44 int main()
45 {
46     scanf("%d",&n);
47     scanf("%s",trophy);
48     printf("%d\n",Solve());
49 }

  xiaokai的思路:

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

原文地址:https://www.cnblogs.com/violet-acmer/p/10045166.html

时间: 2024-10-06 09:17:42

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

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm

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 seconds memory limit per test 256 megabytes input standard input output standard output A multi-subject competition is coming! The competition has mm 

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 standard input output standard output A multi-subject competition is coming! The competition has mm different subjects participants can choose from.

[Educational Codeforces Round 55 (Rated for Div. 2)][C. Multi-Subject Competition][时间复杂度]

https://codeforc.es/contest/1082/problem/C 题目大意:有m个类型,n个人,每个人有一个所属类型k和一个能力v,要求所选的类型的人个数相等并且使v总和最大(n,m<=1e5) 题解:用vector存下每种类型的各个v并且每种类型根据v按从大到小排序,然后处理出每种类型的前缀和,然后扫每种类型的所有前缀和,如果该类型在i处的前缀和大于0,则相应的ans[i]加上这个类型在i处的前缀和,最后求出max(ans[i])(1<=i<=n)即可. 注意:这题

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 input output standard output Vasya is reading a e-book. The file of the book consists of nn pages, numbered from 11 to nn. The screen is currently disp

Codeforces 1082 D. Maximum Diameter Graph-树的直径-最长链-构造题 (Educational Codeforces Round 55 (Rated for Div. 2))

D. Maximum Diameter Graph time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Graph constructive problems are back! This time the graph you are asked to build should match the following proper

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

这道题比赛时候没做出来,下来一看才发现是排序傻逼题. 把每个偏好的人做成一个vector,从大到小排序,做一个前缀和.然后将每种人数做一个桶,在桶里装每种科目选择人数为i的时候分数总和. 遍历每一维vector,把各个位置上面的vector加到sum数组中,最后sum数组里面挑出最大值. #include<bits/stdc++.h> using namespace std; typedef long long ll; vector<int> vec[100010];//vecto

Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition (实现,贪心,排序)

C. Multi-Subject Competition time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output A multi-subject competition is coming! The competition has m different subjects participants can choose from. That'

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ