Codeforces 1082B Vova and Trophies 模拟,水题,坑 B
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
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的更多相关文章
- Codeforces 1082B Vova and Trophies(前缀+后缀)
题目链接:Vova and Trophies 题意:给定长度为n的字符串s,字符串中只有G和S,只允许最多一次操作:任意位置的两个字符互换.求连续G的最长长度. 题解:维护pre和pr,nxt和nx. ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- POJ 2014:Flow Layout 模拟水题
Flow Layout Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3091 Accepted: 2148 Descr ...
- Educational Codeforces Round 7 B. The Time 水题
B. The Time 题目连接: http://www.codeforces.com/contest/622/problem/B Description You are given the curr ...
- Educational Codeforces Round 7 A. Infinite Sequence 水题
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/622/problem/A Description Consider the ...
- Codeforces Testing Round #12 A. Divisibility 水题
A. Divisibility Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
- Codeforces Beta Round #37 A. Towers 水题
A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...
- codeforces 677A A. Vanya and Fence(水题)
题目链接: A. Vanya and Fence time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CodeForces 690C1 Brain Network (easy) (水题,判断树)
题意:给定 n 条边,判断是不是树. 析:水题,判断是不是树,首先是有没有环,这个可以用并查集来判断,然后就是边数等于顶点数减1. 代码如下: #include <bits/stdc++.h&g ...
随机推荐
- Pascal语言(存档)
数据类型 标准函数 运算符和表达式 输入语句 输出语句 if语句 case语句 for语句 while语句 repeat语句 函数与过程 形参与实参 全局变量与局部变量 数组 字符串 枚举 子界 集合 ...
- 应用程序与驱动程序通信 DeviceIoControl
之前写过一篇关于通过DeviceIoControl函数来使应用程序与驱动程序通信的博客,这次再通过这个完整的代码来简要疏通总结一下. 这种通信方式,就是驱动程序和应用程序自定义一种IO控制码,然后调用 ...
- 保持ssh连接长时间不断开的技巧
我经常用ssh连接服务器,过段时间不用, 需要恢复一下断开的连接, 原因是NAT防火墙喜欢对空闲的会话进行超时处理,以确保它们状态表的干净和内存的低占用率,因为 长时间保持连接, 会长期占用部分系统资 ...
- 1. redis安装(windows)
Redis在windows下安装过程 转载自(http://www.cnblogs.com/M-LittleBird/p/5902850.html) 一.下载windows版本的Redis 去官网 ...
- 将LinkedHashMap转换为需要的对象
项目中,在获取json数据转换为list类型以后,本来以为可以直接使用,结果在使用中报错“java.lang.ClassCastException: java.util.LinkedHashMap c ...
- 如何配置IIS使其支持APK文件的下载
在管理工具里打开Internet 信息服务(IIS)管理器.然后选择需要配置的网站. 右侧的界面中会显示该网站的所有功能配置,我们选择并点击进入“MIME类型” 在左侧的操作区选择点击“添加”MIME ...
- API接口认证
restful API接口可以很方便的让其他系统调用,为了让指定的用户/系统可以调用开放的接口,一般需要对接口做认证; 接口认证有两种方式: 1.认证的token当做post/get的参数,serve ...
- 合并数组,改变原数组apply与不改变原数组
一看见合并数组,可能第一反应就是concat,concat确实具有我们想要的行为,但它实际上并不附加到现有数组,而是创建并返回一个新数组. 同样你也许会想到ES6的扩展运算符... 但 ...
- javascript中的getter和setter
在ECMAScript 5中,属性值可以用一个或两个方法代替,这两个方法就是getter和setter var man = { name : 'lidg', weibo : '@lidg', get ...
- scrapy使用MongoDB简单示例
1.下载安装MongoDBhttps://www.mongodb.com/download-center#community找到合适的版本下载,安装.安装好之后,找到安装目录下D:\Program F ...