题目链接:

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(括号匹配转化为折线处理)的更多相关文章

  1. Codeforces 1239B. The World Is Just a Programming Task (Hard Version)

    传送门 这一题好妙啊 首先把括号序列转化成平面直角坐标系 $xOy$ 上的折线,初始时折线从坐标系原点 $(0,0)$ 出发 如果第 $i$ 个位置是 '(' 那么折线就往上走一步($y+1$),否则 ...

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

  3. Codeforces 5C Longest Regular Bracket Sequence(DP+括号匹配)

    题目链接:http://codeforces.com/problemset/problem/5/C 题目大意:给出一串字符串只有'('和')',求出符合括号匹配规则的最大字串长度及该长度的字串出现的次 ...

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

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

  6. Codeforces 918C The Monster(括号匹配+思维)

    题目链接:http://codeforces.com/contest/918/problem/C 题目大意:给你一串字符串,其中有'('.')'.'?'三种字符'?'可以当成'('或者')'来用,问该 ...

  7. 并不对劲的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+' ...

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

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

随机推荐

  1. C# 弹出层移动

    groupPrint.MouseDown += GroupBox1_MouseDown; #region 弹出层移动        [System.Runtime.InteropServices.Dl ...

  2. Django 报错总结

    报错: AttributeError: 'NoneType' object has no attribute 'split' 最近在写网站中遇到一个问题,就是题目上所写的:AttributeError ...

  3. EntityFramework进阶(一)- DbContext与ObjectContext互转

    本系列原创博客代码已在EntityFramework6.0.0测试通过,转载请标明出处 EF中我们常用的是DbContext作为上下文,如果要想获取元数据等信息还是要用到ObjectContext这个 ...

  4. 为满足中国税改,SAP该如何打SPS

    *****一定要先阅读这个note***** ***** 2736625 - [ZH] 应对2019中国个税改革,SAP系统升级常见问题汇总 **** 1784328 - How to check C ...

  5. python实用库

    参考:https://github.com/programthink/opensource/blob/master/libs/python.wiki#35_ Python 开源库及示例代码 Table ...

  6. 更改和配置本地yum源

    查看yum 存放位置 [root@web01 yum.repos.d]# ll /etc/yum.repos.d/ total -rw-r--r--. root root Jun CentOS-Bas ...

  7. 个性化召回算法实践(一)——CF算法

    协同过滤推荐(Collaborative Filtering Recommendation)主要包括基于用户的协同过滤算法与基于物品的协同过滤算法. 下面,以movielens数据集为例,分别实践这两 ...

  8. golang 时间的比较,time.Time的初始值?

    参考: https://golangcode.com/checking-if-date-has-been-set/ https://stackoverflow.com/questions/209243 ...

  9. glog的安装使用

    参考 :https://blog.csdn.net/Pig_Pig_Bang/article/details/81632962 https://blog.csdn.net/cywtd/article/ ...

  10. LOJ 2452 对称 Antisymmetry——用hash求回文串数

    概念 用hash求最长回文串/回文串数 首先,易知,回文串具有单调性. 如果字符串 $s[l...r]$ 为回文串串,那么 $s[x...y]$($l < x, y < r$ 且 $|l- ...