codeforces#1248D2. The World Is Just a Programming Task(括号匹配转化为折线处理)
题目链接:
http://codeforces.com/contest/1248/problem/D2
题意:
可以执行一次字符交换的操作
使得操作后的字符串,循环移位并且成功匹配的方案最多
输出最多的方案数和交换的位置
数据范围:
$1\leq n \leq 300 000$
分析:
参考博客:https://www.cnblogs.com/LLTYYC/p/11718968.html
对于一个字符串,求它的循环移位匹配方案数
可以把字符串转换成折线,从0开始,遇到(则加一,遇到)则减一
如果(和)数量不同,明显答案是0,否则,答案是折线y最小值的顶点数量
可以偏移一下,使得最小值为0,那么整体折线向上偏移
如果折线归0,答案加一
那么考虑可以交换一次的情况
先对折线向上偏移
答案是1的数量或者 2的数量加不交换的答案,即,更改最小值为-1,或者增加0的数量,两种操作方案
其中区间1的数量最小值是1,区间2的数量同理
AC代码:
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int maxn=3e5+7;
int n;
char S[maxn];
int val[maxn];
int main(){
scanf("%d",&n);
scanf("%s",S+1);
int off=0,minn=1e9;
int now=0;
for(int i=1;i<=n;i++){
if(S[i]==')')now--;
else now++;
if(minn>now){
minn=now;
off=i;
}
}
if(now!=0){
printf("0\n1 1\n");
return 0;
}
int ans=0,ansl=1,ansr=1,anss=0;
for(int i=1;i<=n-1;i++){
val[i+1]=val[i];
int v=(off+i-1)%n+1;
if(S[v]==')')val[i+1]--;
else val[i+1]++;
if(val[i]==0)ans++;
//cout<<val[i]<<" ";
}
// coutMM
anss=ans;
for(int i=1;i<=n+1;i++){
if(val[i]==2){
int en=i+1,tem=1;
while(val[en]>=2){
if(val[en]==2)tem++;
en++;
}
if(tem+anss>ans){
ans=tem+anss;
ansl=(i+off-2+n)%n+1;
ansr=(en+off-2+n)%n+1;
}
i=en;
}
}
for(int i=1;i<=n+1;i++){
//cout<<val[i]<<" ";
if(val[i]==1){
int en=i+1,tem=1;
while(val[en]>=1){
if(val[en]==1)tem++;
en++;
}
if(tem>ans){
ans=tem;
ansl=(i+off-2+n)%n+1;
ansr=(en+off-2+n)%n+1;
}
i=en;
}
} printf("%d\n%d %d\n",ans,ansl,ansr);
return 0;
}
codeforces#1248D2. The World Is Just a Programming Task(括号匹配转化为折线处理)的更多相关文章
- Codeforces 1239B. The World Is Just a Programming Task (Hard Version)
传送门 这一题好妙啊 首先把括号序列转化成平面直角坐标系 $xOy$ 上的折线,初始时折线从坐标系原点 $(0,0)$ 出发 如果第 $i$ 个位置是 '(' 那么折线就往上走一步($y+1$),否则 ...
- Codeforces Round #594 (Div. 1) D2. The World Is Just a Programming Task (Hard Version) 括号序列 思维
D2. The World Is Just a Programming Task (Hard Version) This is a harder version of the problem. In ...
- Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)
题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树
E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...
- C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)
冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C C. Serval and Parenthesis Sequence time limit pe ...
- Codeforces 918C The Monster(括号匹配+思维)
题目链接:http://codeforces.com/contest/918/problem/C 题目大意:给你一串字符串,其中有'('.')'.'?'三种字符'?'可以当成'('或者')'来用,问该 ...
- 并不对劲的CF1239B&C&D Programming Task in the Train to Catowice City
CF1239B The World Is Just a Programming Task 题目描述 定义一个括号序列s是优秀的,当且仅当它是以下几种情况的一种: 1.|s|=0 2.s='('+t+' ...
- Codeforces 801 A.Vicious Keyboard & Jxnu Group Programming Ladder Tournament 2017江西师大新生赛 L1-2.叶神的字符串
A. Vicious Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Gym100814 F.Geometry (ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (2015) Arab Academy for Science and Technology)
这个题真的是超级超级水啊,哈哈哈哈哈哈.不要被题面吓到,emnnn,就这样... 代码: 1 #include<iostream> 2 #include<cstring> 3 ...
随机推荐
- 【SoloPi】SoloPi使用1-初次使用,环境准备(Windows版)
下载配置Android SDK路径前往 https://developer.android.com/studio/releases/platform-tools#downloads 下载对应系统版本的 ...
- C# 字符串按设置的格试在前面或后面增加空格或其它字符
public string lengadd(string stringa, string stringb, int count, int mode) //以stringa的长度,未到count的长度则 ...
- rem em min-width: 30em 的意思
30em=30rem=30x16px=480px @media only screen and (min-width:30 em){ }
- Vivado cordic IP求模求角教程
前言 当需要对复数求模的时候,用FPGA怎么求呢?怎么开根号? 方法1:先求幅值平方和,再使用cordic IP开根号.(蠢办法) 方法2:直接用cordic求取模值. 此处只介绍方法2,资源占用更少 ...
- Hive调优笔记
Hive调优 先记录了这么多,日后如果有遇到,再补充. fetch模式 <property> <name>hive.fetch.task.conversion</name ...
- Linux 之 文件
文件名称 在linux中,windows概念中的文件夹和文件是没有区别的,都是统称为文件. 1.Linux中文件的名称大小写是敏感的 2.名称最多可以为255个字符 3.除了正斜线以外,都是有效字符 ...
- 利用 Python django 框架 输入汉字,数字,字符,等。。转成二维码!
利用 Python django 框架 输入汉字,数字,字符,等..转成二维码! 模块必备:Python环境 + pillow + qrcode 模块 核心代码import qrcode qr = ...
- 记录java+testng运行selenium(四)--- 结构说明
一图:主要是driver文件所在目录,及ini配置文件所在位置. 这两个文件一般我是放在其它目录下,不跟随项目所在目录 二图:用例操作类及用例执行类所在位置. 下图中有接口代码及功能代码组成,之前的文 ...
- linux防火墙扩展模块实战(二)
iptables扩展模块 扩展匹配条件:需要加载扩展模块(/usr/lib64/xtables/*.so),方可生效 查看帮助 man iptables-extensions (1)隐式扩展 ...
- Python Multiprocessing 多进程,使用多核CPU计算 并使用tqdm显示进度条
1.背景 在python运行一些,计算复杂度比较高的函数时,服务器端单核CPU的情况比较耗时,因此需要多CPU使用多进程加快速度 2.函数要求 笔者使用的是:pathos.multiproces ...