Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】

探讨了在一次交换机会下,如何通过贪心算法找到最长连续'G'序列的解决方案,该问题源自Codeforces竞赛。

传送门:http://codeforces.com/contest/1082/problem/B

B. Vova and Trophies
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vova has won 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 nn (2n1052≤n≤105) — the number of trophies.

The second line contains nn characters, each of them is either G or S. If the ii-th character is G, then the 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.

Examples
input
Copy
10
GGGSGGGSGG
output
Copy
7
input
Copy
4
GGGG
output
Copy
4
input
Copy
3
SSS
output
Copy
0
Note

In the first example Vova has to swap trophies with indices 44 and 1010. Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is 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 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 00.

 

题意概括:

给一串只含有 G 和 S 的字符串,有一次将两个字符对调位置的机会,求最长的连续 'G' 序列的长度。

解题思路:

想得太多系列,一开始用了二分搜索,debug到爆炸。

其实就是一个贪心:记录中间 ‘S’ 前面的 ‘G’的长度 和 后面 'G' 的长度,作和。取最大值 maxlen。最后答案和 总的‘G’数量 snt 进行一下比较,如果大了说明多加的,输出 snt,否则输出 maxlen。

 

AC code:

  1 //#include <cstdio>
  2 //#include <iostream>
  3 //#include <cstring>
  4 //#include <algorithm>
  5 //#include <cmath>
  6 //#include <map>
  7 //#define INF 0x3f3f3f3f
  8 //#define LL long long
  9 //using namespace std;
 10 //const int MAXN = 1e5+10;
 11 //char str[MAXN];
 12 //int N, snt;
 13 //
 14 //bool check(int len)
 15 //{
 16 //    bool flag = false;
 17 //    int sum = 0, k = 0;
 18 //    int lst = 0;
 19 //    for(int i = 0; i < N; i++){
 20 //        if(str[i] == 'S' &&  i != 0 && str[i-1] == 'S'){
 21 //            sum = 0;k = 0;lst = 0;
 22 //        }
 23 //        if(str[i] == 'S' && !flag && str[i+1] == 'G' && i < N-1){
 24 //            flag = true;
 25 //            //printf("sum:%d\n", sum);
 26 //            lst = sum;
 27 //            if(sum <= snt)
 28 //                sum++;
 29 //            continue;
 30 //        }
 31 //        else if(str[i] == 'S' && flag == true && str[i-1] != 'S'){
 32 //            //printf("len:%d sum:%d\n",len, sum);
 33 //            if(sum >= len) return true;
 34 //            sum-=lst;
 35 //            lst=k;
 36 //            k=0;
 37 //        }
 38 //        else if(str[i] == 'G' && sum <= snt){
 39 //            //printf("len:%d i:%d sum:%d\n", len, i , sum);
 40 //            sum++;
 41 //            k++;
 42 //            if(sum >= len) return true;
 43 //        }
 44 //    }
 45 //    //printf("len:%d sum:%d\n", len, sum);
 46 //    //if(sum >= len) return true;
 47 //    return false;
 48 //}
 49 //
 50 //int main()
 51 //{
 52 //    scanf("%d", &N);
 53 //    scanf("%s", str);
 54 //    snt = 0;
 55 //    for(int i = 0; i < N; i++){
 56 //        if(str[i] == 'G') snt++;
 57 //    }
 58 //    //printf("%d\n", snt);
 59 //    int l = 0, r = N;
 60 //    int mid = 0, ans = 0;
 61 //    while(l<=r){
 62 //        //printf("mid:%d\n", mid);
 63 //        mid = (l+r)>>1;
 64 //        if(check(mid)) {
 65 //                l = mid+1;
 66 //                ans=mid;
 67 //        }
 68 //        else r = mid-1;
 69 //    }
 70 //    //printf("l:%d snt:%d\n", l, snt);
 71 //    if(ans == snt+1) ans = snt;
 72 //    printf("%d\n", ans);
 73 //    return 0;
 74 //}
 75 
 76 
 77 #include <cstdio>
 78 #include <iostream>
 79 #include <algorithm>
 80 #include <cmath>
 81 #include <cstring>
 82 #define INF 0x3f3f3f3f
 83 #define LL long long
 84 using namespace std;
 85 const int MAXN = 1e5+10;
 86 char str[MAXN];
 87 int N;
 88 int main()
 89 {
 90     scanf("%d", &N);
 91     scanf("%s", str);
 92     int gg = 0;
 93     int g = 0, k = 0;
 94     int len = 0;
 95 
 96     for(int i = 0; i < N; i++){
 97         if(str[i] == 'G'){
 98             g++;
 99             k++;
100         }
101         else{
102             gg = g;
103             g = 0;
104         }
105         len = max(len, gg+g+1);
106     }
107     //printf("%d %d\n", len, k);
108     int ans = min(len, k);
109     printf("%d\n", ans);
110     return 0;
111 }
View Code

 

【太阳能学报EI复现】基于粒子群优化算法的风-水电联合优化运行分析(Matlab代码实现)内容概要:本文档是一份关于“基于粒子群优化算法的风-水电联合优化运行分析”的研究资料,旨在通过Matlab代码实现对该优化模型的复现。文档重点介绍了如何利用粒子群优化(PSO)算法解决风能与水能联合调度中的复杂优化问题,包括系统建模、目标函数构建、约束条件处理及算法实现过程。研究兼顾可再生能源的不确定性与电力系统运行的经济性,通过仿真验证了该方法在提升能源利用率和系统稳定性方面的有效性。此外,文档还附带多个相关领域的Matlab代码案例,涵盖微电网调度、储能配置、负荷预测等,突出其在电力系统优化中的实际应用价值。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事新能源优化调度的工程技术人员;尤其适合希望复现EI期刊论文或开展智能优化算法在能源领域应用研究的用户。; 使用场景及目标:①学习并复现基于粒子群算法的风-水电联合运行优化模型;②掌握Matlab在电力系统优化中的建模与仿真方法;③拓展至微电网、储能调度、多能源协同优化等相关课题的研究与开发。; 阅读建议:建议结合文档中提供的Matlab代码进行逐模块调试与分析,重点关注目标函数设计、粒子群算法参数设置及约束处理机制。同时可参考文中列举的其他优化案例,举一反三,提升对智能算法在能源系统中综合应用的理解与实践能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值