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 memory limit per test 256 megabytes input standard input output standard outputVova 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.
Examples input Copy10 GGGSGGGSGGoutput Copy
7input Copy
4 GGGGoutput Copy
4input Copy
3 SSSoutput Copy
0Note
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