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. 理解Linux系统负荷load average

    理解Linux系统负荷   一.查看系统负荷 如果你的电脑很慢,你或许想查看一下,它的工作量是否太大了. 在Linux系统中,我们一般使用uptime命令查看(w命令和top命令也行).(另外,它们在 ...

  2. 常见的php模式

    php中6种常见的设计模式 单例模式 观察者模式 策略模式 工厂模式 注册模式 适配器模式 单例模式 Db.php<?php /** * 单例模式 */ class Db { private s ...

  3. Postgres——pgadmin复制无主键单表至本地数据库

    数据库中存在无主键单表gongan_address_all ,需要将余杭区数据导出成另外一张表,因为数据量太大,sql语句效率太差. 通过sql语句查询出余杭区数据,并导出成csv,sql等格式,再导 ...

  4. Android 开发 框架系列 Android-Universal-Image-Loader 图片加载使用demo

    Android-Universal-Image-Loader github地址:https://github.com/nostra13/Android-Universal-Image-Loader 加 ...

  5. 第三方jar上传到Maven私服(Nexus)

    mvn deploy:deploy-file -DgroupId=taobao-sdk -DartifactId=taobao-sdk-java -Dversion=1.0 -Dpackaging=j ...

  6. spring 之 注入之 by name or by type, or both ?

    @Autowired 和  @Qualifier 使用xml 注入的时候, 我们可以指定 autowire=“byType” 或“byName” . 但是使用 注解的时候, @Autowired  只 ...

  7. python大法好——模块(内置模块未完)

    模块 模块是非常简单的Python文件,单个Python文件就是一个模块,两个文件就是两个模块. Python模块有什么作用? 1.模块内有许多函数方法,利用这些方法可以更简单的完成许多工作.2.模块 ...

  8. centos7安装mysql客户端

    1.判断是否已安装 [root@k8s-master master]# which mysql /usr/bin/which: no mysql in (/usr/local/sbin:/usr/lo ...

  9. Sphinx 与全文索引

    全文索引创建过程 第一步:将源文档传给分词组件(Tokenizer) 分词组件做了以下事情: 将文档分成一个一个的单词 去除标点符号 去除停词:英文(the / a / this / that ... ...

  10. Eclipse 安装中文简体语言包

    Installing the language packs Open the install wizard with 'Help' > 'Install new software...' Add ...