Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】
传送门:http://codeforces.com/contest/1082/problem/B
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.
The first line contains one integer nn (2≤n≤1052≤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.
Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.
10 GGGSGGGSGG
7
4 GGGG
4
3 SSS
0
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:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步