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

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代码:

 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <time.h>
#include <queue>
#include <string.h>
#include <list>
#define sf scanf
#define pf printf
#define lf double
#define ll long long
#define p123 printf("123\n");
#define pn printf("\n");
#define pk printf(" ");
#define p(n) printf("%d",n);
#define pln(n) printf("%d\n",n);
#define s(n) scanf("%d",&n);
#define ss(n) scanf("%s",n);
#define ps(n) printf("%s",n);
#define sld(n) scanf("%lld",&n);
#define pld(n) printf("%lld",n);
#define slf(n) scanf("%lf",&n);
#define plf(n) printf("%lf",n);
#define sc(n) scanf("%c",&n);
#define pc(n) printf("%c",n);
#define gc getchar();
#define re(n,a) memset(n,a,sizeof(n));
#define len(a) strlen(a)
#define f(i,n) for(int i = 0; i < n; i ++)
#define LL long long
#define eps (1e-6)
using namespace std;
char a[];
int num[];
int main() {
int n ;
s(n);
ss(a)
re(num,);
int count0 = ;
f(i,n) {
if(a[i] == 'S') {
if(num[count0] != ) {
count0 += ;
} else {
count0 ++;
}
} else if(a[i] == 'G') {
num[count0] ++;
}
}
int count1 = ;
for(int i = ; i <= count0; i ++) {
if(num[i] != ) {
count1 ++;
}
}
if(count1 == ) {
for(int i = ; i <= count0; i ++) {
if(num[i] != ) {
p(num[i]) pn return ;
}
}
} else if(count1 == ) {
int maxi = ;
for(int i = ; i <= count0; i ++) {
if(num[i] != ) {
if(num[i] != && num[i+] == && num[i+] != ) {
p(num[i]+num[i+]) pn return ;
}
if(maxi < num[i]) {
maxi = num[i];
}
}
}
p(maxi) pn return ;
} else {
int maxi = ;
for(int i = ; i <= count0; i ++) {
if(num[i] != && num[i+] == && num[i+] != ) {
if(maxi < num[i]+num[i+]+) {
maxi = num[i]+num[i+]+;
}
}
}
p(maxi) pn return ;
} return ;
}

最后小小的皮了一下,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.

 #include <bits/stdc++.h>
using namespace std;
int n;
string s;
int main() { cin >> n >> s; vector <int> l(n), r(n);
for(int i = ; i < n; ++i){
if(s[i] == 'G'){
l[i] = ;
if(i > ) l[i] += l[i - ];
}
}
for(int i = n - ; i >= ; --i){
if(s[i] == 'G'){
r[i] = ;
if(i + < n) r[i] += r[i + ];
}
} int res = ;
int cntG = ;
for(int i = ; i < n; ++i)
cntG += s[i] == 'G'; for(int i = ; i < n; ++i){
if(s[i] == 'G') continue;
int nres = ;
if(i > ) nres += l[i - ];
if(i + < n) nres += r[i + ];
res = max(res, nres);
} res = min(res, cntG);
if(cntG == n) res = cntG;
cout << res << endl;
return ;
}

Codeforces 1082B Vova and Trophies 模拟,水题,坑 B的更多相关文章

  1. Codeforces 1082B Vova and Trophies(前缀+后缀)

    题目链接:Vova and Trophies 题意:给定长度为n的字符串s,字符串中只有G和S,只允许最多一次操作:任意位置的两个字符互换.求连续G的最长长度. 题解:维护pre和pr,nxt和nx. ...

  2. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  3. POJ 2014:Flow Layout 模拟水题

    Flow Layout Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3091   Accepted: 2148 Descr ...

  4. Educational Codeforces Round 7 B. The Time 水题

    B. The Time 题目连接: http://www.codeforces.com/contest/622/problem/B Description You are given the curr ...

  5. Educational Codeforces Round 7 A. Infinite Sequence 水题

    A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...

  6. Codeforces Testing Round #12 A. Divisibility 水题

    A. Divisibility Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...

  7. Codeforces Beta Round #37 A. Towers 水题

    A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...

  8. codeforces 677A A. Vanya and Fence(水题)

    题目链接: A. Vanya and Fence time limit per test 1 second memory limit per test 256 megabytes input stan ...

  9. CodeForces 690C1 Brain Network (easy) (水题,判断树)

    题意:给定 n 条边,判断是不是树. 析:水题,判断是不是树,首先是有没有环,这个可以用并查集来判断,然后就是边数等于顶点数减1. 代码如下: #include <bits/stdc++.h&g ...

随机推荐

  1. ubuntu14.04 安装jdk1.8及以上

    安装xmind1.8 碰到jvm版本太低的错误: Version 1.7.0_131 of the JVM is not suitable for this product. Version: 1.8 ...

  2. 涂抹mysql笔记-数据备份和恢复

    <>物理备份和逻辑备份<>联机备份和脱机备份<>本地备份和远程备份<>完整备份和增量备份<>完整恢复和增量恢复<>复制表相关文件 ...

  3. 第26课 可变参数模板(7)_any和variant类的实现

    1. any类的实现 (1)any类: ①是一个特殊的,只能容纳一个元素的容器,它可以擦除类型,可以将何任类型的值赋值给它. ②使用时,需要根据实际类型将any对象转换为实际的对象. (2)实现any ...

  4. 关于java和jvm的思考

    1. 多线程并发访问一个静态方法(static method)会不会有什么性能影响? 思考:不会,原因是static method编译后及时一串指令(jvm中的静态方法区,也叫non-heap(包含了 ...

  5. (转)EF5+SQLserver2012迁移到EF6+mysql5.5.47

    原文地址:https://www.cnblogs.com/tinyjian/p/6235014.html:https://www.cnblogs.com/tinyjian/p/6235397.html ...

  6. HTML span标签:用来组合文档中的行内元素

    在DIV+CSS切图布局重构技术中,除了常常使用div标签外也常常使用span标签布局,通常也可以通过对span标签对象设置不同样式实现我们要的美化效果.这里主机吧主要讲的是span标签的定义和用法. ...

  7. cv2的安装

    第一种 ,直接尝试 pip install cv2 ,大可能报错. 第二种,pip install opencv-python ,大概率 直接成功. 第三种 ,去网上下包 放到 sit_package ...

  8. servlet cdi注入

    @WebServlet("/cdiservlet")//url映射,即@WebServlet告诉容器,如果请求的URL是"/cdiservlet",则由NewS ...

  9. Linux实操篇 vi和vim编辑器

    vi和vim的基本介绍 所有的Linux系统都会内建vi文本编辑器. vim具有程序编辑的能力,可以看做是vi的增强版本,可以主动的以字体颜色辨别语法的正确性,方便程序设计.代码补完.编译及错误跳转等 ...

  10. T-SQL中CTE表 with关键字

    Select字句在逻辑上是SQL语句最后进行处理的最后一步,所以,以下查询会发生错误: SELECT YEAR(OrderDate) AS OrderYear, COUNT(DISTINCT Cust ...