热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Codeforces1082BVovaandTrophies模拟,水题,坑B

Codeforces1082BVovaandTrophieshttps:vjudge.netproblemCodeForces-1082B题目:Vovahaswon nn trop

Codeforces 1082B Vova and Trophies

https://vjudge.net/problem/CodeForces-1082B

题目:

   

    Vova has won n">nn trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row.

     The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible — that means, to maximize the length of the longest such subsegment.

    Help Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap.

Input

    The first line contains one integer n">nn (2≤n≤105">2n1052≤n≤105) — the number of trophies.

    The second line contains n">nn characters, each of them is either G or S. If the i">ii-th character is G, then the i">ii-th trophy is a golden one, otherwise it‘s a silver trophy.

Output


    Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.

0">Examples


Input1


10
GGGSGGGSGG


Output1


7


Input2


4
GGGG


Output2


4


Input3


3
SSS


Output3


0


Note

    

    In the first example Vova has to swap trophies with indices 4">44 and 10">1010. Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is 7">77.

    In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is 4">44.

    In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than 0">00.

技术分享图片

 

 

分析:

标准水题,真的是标准水题
but需要分类
分类还特别恶心
然后比赛ing被光荣的hack了
然后又wa了一堆
居然有三个点没有注意
当有多个连续区间时可以移动其他的来补充最长的使最长的+1
当没有连续区间时输出0
当有两个连续区间的时候同第一个点,可以移动其他的来补充最长的
hack代码:

1 #include
2 #include
3 #include <string.h>
4 #include
5 #include
6 #include <string>
7 #include
8 #include
9 #include <string.h>
10 #include
11 #define sf scanf
12 #define pf printf
13 #define lf double
14 #define ll long long
15 #define p123 printf("123\n");
16 #define pn printf("\n");
17 #define pk printf(" ");
18 #define p(n) printf("%d",n);
19 #define pln(n) printf("%d\n",n);
20 #define s(n) scanf("%d",&n);
21 #define ss(n) scanf("%s",n);
22 #define ps(n) printf("%s",n);
23 #define sld(n) scanf("%lld",&n);
24 #define pld(n) printf("%lld",n);
25 #define slf(n) scanf("%lf",&n);
26 #define plf(n) printf("%lf",n);
27 #define sc(n) scanf("%c",&n);
28 #define pc(n) printf("%c",n);
29 #define gc getchar();
30 #define re(n,a) memset(n,a,sizeof(n));
31 #define len(a) strlen(a)
32 #define f(i,n) for(int i = 0; i 33 #define LL long long
34 #define eps (1e-6)
35 using namespace std;
36 char a[1000000];
37 int num[1000000];
38 int main() {
39 int n ;
40 s(n);
41 ss(a)
42 re(num,0);
43 int count0 = 0;
44 f(i,n) {
45 if(a[i] == S) {
46 if(num[count0] != 0) {
47 count0 += 2;
48 } else {
49 count0 ++;
50 }
51 } else if(a[i] == G) {
52 num[count0] ++;
53 }
54 }
55 int count1 = 0;
56 for(int i = 0; i <= count0; i ++) {
57 if(num[i] != 0) {
58 count1 ++;
59 }
60 }
61 if(count1 == 1) {
62 for(int i = 0; i <= count0; i ++) {
63 if(num[i] != 0) {
64 p(num[i]) pn return 0;
65 }
66 }
67 } else if(count1 == 2) {
68 int maxi = 0;
69 for(int i = 0; i <= count0; i ++) {
70 if(num[i] != 0) {
71 if(num[i] != 0 && num[i+1] == 0 && num[i+2] != 0) {
72 p(num[i]+num[i+2]) pn return 0;
73 }
74 if(maxi < num[i]) {
75 maxi = num[i];
76 }
77 }
78 }
79 p(maxi) pn return 0;
80 } else {
81 int maxi = 0;
82 for(int i = 0; i <= count0; i ++) {
83 if(num[i] != 0 && num[i+1] == 0 && num[i+2] != 0) {
84 if(maxi 2]+1) {
85 maxi = num[i]+num[i+2]+1;
86 }
87 }
88 }
89 p(maxi) pn return 0;
90 }
91
92 return 0;
93 }



 技术分享图片

技术分享图片

最后小小的皮了一下,wa on test193
附上标答和标解
1082B - Vova and Trophies
Let riri be the maximal segment of gold cups that begins in the cup ii. Let lili be the maximum segment of gold cups that ends in the cup ii. Also, let the total number of gold cups be cntGcntG.
Note that it makes no sense to change the cups of the same color. Then let‘s consider the silver cup, which will change with the gold cup, let its number be ii. Then if ri+1+li?1


 

1 #include
2 using namespace std;
3 int n;
4 string s;
5 int main() {
6
7 cin >> n >> s;
8
9 vector <int> l(n), r(n);
10 for(int i = 0; i i){
11 if(s[i] == G){
12 l[i] = 1;
13 if(i > 0) l[i] += l[i - 1];
14 }
15 }
16 for(int i = n - 1; i >= 0; --i){
17 if(s[i] == G){
18 r[i] = 1;
19 if(i + 1 1];
20 }
21 }
22
23
24 int res = 0;
25 int cntG = 0;
26 for(int i = 0; i i)
27 cntG += s[i] == G;
28
29 for(int i = 0; i i){
30 if(s[i] == G) continue;
31 int nres = 1;
32 if(i > 0) nres += l[i - 1];
33 if(i + 1 1];
34 res = max(res, nres);
35 }
36
37 res = min(res, cntG);
38 if(cntG == n) res = cntG;
39 cout < endl;
40 return 0;
41 }

 


推荐阅读
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • 本文介绍了指针的概念以及在函数调用时使用指针作为参数的情况。指针存放的是变量的地址,通过指针可以修改指针所指的变量的值。然而,如果想要修改指针的指向,就需要使用指针的引用。文章还通过一个简单的示例代码解释了指针的引用的使用方法,并思考了在修改指针的指向后,取指针的输出结果。 ... [详细]
  • 在project.properties添加#Projecttarget.targetandroid-19android.library.reference.1..Sliding ... [详细]
author-avatar
好kc好先生之家
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有