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 ...
随机推荐
- Vue 路由及路由默认跳转
路由就是让根组件动态得去挂载其他组件: 步骤: //路由配置: //.安装 npm install vue-router --save / cnpm install vue-router --save ...
- Vue 双向数据绑定、事件介绍以及ref获取dom节点
vue是一个MVVM的框架 M model V view MVVM model改变会影响视图view,view改变会影响model 双向数据绑定必须在表单里面使用 //我发现在谷歌浏览器翻译后的网页 ...
- vue 一些可以优化的地方
第一招:化繁为简的Watchers 场景还原: created(){ this.fetchPostList() }, watch: { searchInputValue(){ this.fetchPo ...
- android 开发 View _15 导入一张图片将它裁剪成圆形 与 paint图层叠加处理详解
方法一: /* 实现思维是这样的: 1.首先拿到bitmap图片 2.得到bitmap图片的高度 宽度,并且计算好各个画图尺寸 3.创建一个空白的 bitmap图片: Bitmap output = ...
- shareSdk分享图片
/** * 只分享图片 * * @param bitmap bitmap */ private void sharedImage(Bitmap bitmap) { RxPermissions rxPe ...
- kettle 无法正常启动问题
Java环境配置后,双击spoon.bat无法打开 修改spoon.bat里内存配置: if "%PENTAHO_DI_JAVA_OPTIONS%"=="" s ...
- Python学习:模块初识、数据类型
1.模块初识 在Python中,模块分为两种: (1)标准库 标准库无需安装,只要在使用的时候import就可以使用了 (2)第三方库 第三方库必须下载安装之后才能够引入使用 下面介绍两个基本的模块: ...
- leetcode4
public class Solution { public double FindMedianSortedArrays(int[] nums1, int[] nums2) { var nums = ...
- django-celery使用
1.新进一个django项目 - proj/ - proj/__init__.py - proj/settings.py - proj/urls.py - manage.py 2.在该项目创建一个pr ...
- xml实现登录表单验证
定义: XML(eXtended Markup Language,可扩展标记语言)提供了一套跨平台.跨网络.跨程序的语言的数据描述方式,使用XML可以方便地实现数据交换.系统配置.内容管理等常见功能. ...