Codeforces 1082B Vova and Trophies

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

题目:

   

    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

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 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.

 

 

分析:

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

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <string>
 7 #include <time.h>
 8 #include <queue>
 9 #include <string.h>
10 #include <list>
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 < n; 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 < num[i]+num[i+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<cntGri+1+li−1<cntG, then we will update the answer with the value ri+1+li−1+1ri+1+li−1+1, and otherwise with the value ri+1+li−1ri+1+li−1. This will not work if all the cups are golden. In this case, the answer is nn.

 

 1 #include <bits/stdc++.h>
 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 < n; ++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 < n) r[i] += r[i + 1];
20         }
21     }
22     
23     
24     int res = 0;
25     int cntG = 0;
26     for(int i = 0; i < n; ++i)
27             cntG += s[i] == 'G';
28             
29     for(int i = 0; i < n; ++i){
30         if(s[i] == 'G') continue;
31         int nres = 1;
32         if(i > 0) nres += l[i - 1];
33         if(i + 1 < n) nres += r[i + 1];
34         res = max(res, nres);
35     }
36     
37     res = min(res, cntG);
38     if(cntG == n) res = cntG;
39     cout << res << endl;
40     return 0;
41 }

 

posted on 2018-12-04 15:52  Kidgzz  阅读(297)  评论(0编辑  收藏  举报