codeforces 594
D
给你一个长度为n的括号序列,然后你可以选择交换两个位置,你需要使得能够变成 合法括号序列的起点最多。
题解
人尽皆知的东西:合法的括号序列是,令'('为1,')'为-1,那么前缀和需要>=0,且最后的总和应该为0.
假设现在已经是交换好的序列了,那么答案个数,就是前缀和的最小值的个数。这是因为最小值,例如最小值-1(或者-2),造成这个-1(或者-2)原因就是前面的没有一个1(或者2),那我们就得把前面的-1-2放到后面去,意思就是将最小值的后面第一段作为起点
如果交换'('和')',会使得这个区间的每个数的前缀和-=2,不包括交换点。如果交换')'和'(',你可以改变交换为a[i]和a[j+n],将其变成交换'('和')'
因为我们使得这个区间里面的每个数都减去了2,那么最小值只可能是 区间内的2+区间外的0,或者区间内的1。
所以我们看两种情况,哪种最小即可
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int n;
string s;
int a[maxn],ps[maxn];
int main(){
cin>>n>>s;
for(int i=0;i<s.size();i++){
int p = i+1;
if(s[i]=='('){
a[p]=a[p+n]=1;
}else{
a[p]=a[p+n]=-1;
}
}
for(int i=1;i<=2*n;i++){
ps[i]=ps[i-1]+a[i];
}
if(ps[n]){
cout<<"0"<<endl;
cout<<"1 1"<<endl;
return 0;
}
int beg = min_element(ps+1,ps+1+n)-ps;
int minv = ps[beg];
for(int i=1;i<=2*n;i++){///所有值减去最小值,也就是最低也是从0开始
ps[i]-=minv;
}
int cl = -1, cc = 0;
int mx = 0,ansl = 1,ansr = 1;
int cl2 = 0, cc2 = 0;
int mx2 = 0, ans2l = 1, ans2r = 1;
for(int i=1;i<=n;i++){
if(ps[i+beg]==1){
if(cc>mx){
mx=cc;
ansl=cl,ansr=i;
}
cl=i+1;///这是因为我们遇到的是1,而选择位置3和位置8交换影响的是3 4 5 6 7;
///所以ans1=i+1;ansr=i;
cc=0;
}else if(ps[i+beg]==2)++cc;
///上面这部分表示的是区间里全都是大于等于2的这样减2才可以保证最低的值是0;然后再到下面统计区间外的0;
///同时第一个2肯定是从1进去,最后一个2肯定碰到1,所以上面的if来判断边界 if(ps[i+beg]==0){
if(cc2>mx2){
mx2=cc2;
ans2l=cl2+1,ans2r=i;
}
cl2=i;
cc2=0;
}else if(ps[i+beg]==1)++cc2;
}
for(int i=1;i<=n;i++){
if(ps[i+beg]==0)++mx;///刚开始懵逼万一有的0是我们区间统计2里面的0怎么办呢,然后想一下哦对哦区间2统计的是区间内全是大于等于2的不可能出现0,所以就是区间全大于等于2的-2变为0后+上区间外的0,和区间内的全为1再-2等于-1的比较
}
if(mx2>mx){
mx=mx2;
ansl=ans2l;
ansr=ans2r;
}
cout<<mx<<endl;
cout<<(ansl+beg-1+n)%n+1<<" "<<(ansr+beg-1+n)%n+1<<endl;
}
codeforces 594的更多相关文章
- http://codeforces.com/problemset/problem/594/A
A. Warrior and Archer time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #594 (Div. 2)
传送门 C. Ivan the Fool and the Probability Theory 题意: 给出一个\(n*m\)的方格,现在要给方格中的元素黑白染色,要求任一颜色最多有一个颜色相同的格子 ...
- Codeforces Round #594 (Div. 1) D. Catowice City 图论
D. Catowice City In the Catowice city next weekend the cat contest will be held. However, the jury m ...
- Codeforces Round #594 (Div. 1) C. Queue in the Train 模拟
C. Queue in the Train There are
- 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 Round #594 (Div. 2) B. Grow The Tree 水题
B. Grow The Tree Gardener Alexey teaches competitive programming to high school students. To congrat ...
- Codeforces Round #594 (Div. 2) A. Integer Points 水题
A. Integer Points DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS to ...
- Codeforces Round #594 (Div. 1) A. Ivan the Fool and the Probability Theory 动态规划
A. Ivan the Fool and the Probability Theory Recently Ivan the Fool decided to become smarter and stu ...
- Codeforces Round #594 (Div. 1)
Preface 这场CF真是细节多的爆炸,B,C,F都是大细节题,每道题都写了好久的说 CSP前的打的最后一场比赛了吧,瞬间凉意满满 希望CSP可以狗住冬令营啊(再狗不住真没了) A. Ivan th ...
随机推荐
- MySQL实现主从复制功能
环境说明 centos7.3.MySQL5.7 前言 MySQL安装参考之前的文章https://www.jianshu.com/p/452aa99c7476有讲解. ...
- keep-alive 用法 及activated,deactivated这两个生命周期函数
keep-aliveProps: include - 字符串或正则表达式.只有名称匹配的组件会被缓存.exclude - 字符串或正则表达式.任何名称匹配的组件都不会被缓存.max - 数字.最多可以 ...
- lunix下的redis安装
https://blog.csdn.net/qq_35992900/article/details/82950157
- 5、mysql的连接查询
1.内联查询 >inner join 或 join 2.外联查询 (1)左连接 >left outer join 或 left join (2)右连接 >right outer jo ...
- 「NOIP2013」华容道
传送门 Luogu 解题思路 预支一点东西: 这题其实有着更为思维的图模型,还十分考验码力,不简单啊 这居然是联赛题 讲正解: 显然我们对于一种合法方案,空格子肯定是一直围绕着特定棋子反复横跳的. 所 ...
- 「NOIP2009」靶形数独
传送门 Luogu 解题思路 这题其实挺简单的. 首先要熟悉数独,我们应该要优先搜索限制条件多的行,也就是可能方案少的行,显然这样可以剪枝,然后再发挥一下dfs的基本功就可以了. 细节注意事项 爆搜题 ...
- Mongo2Go 介绍
Mongo2Go(https://github.com/Mongo2Go/Mongo2Go )是最新的MongoDB二进制文件的托管包装, 它针对.NET Standard 1.6(对于.NET 4. ...
- LINQ---查询表达式的结构
重要事项: 子句必须按照一定的顺序出现 from子句和select...group子句这两部分是必须的 其他子句是可选的 在LINQ查询表达式中,select子句在表达式最后. 可以后任意多的from ...
- alsa-utils 的使用
ref : https://blog.csdn.net/outstanding_yzq/article/details/8126350 一.alsa-utils介绍 ALSA是kernel中的一个声 ...
- #$d#$a什么意思
#$d#$a什么意思 qjh.693111级分类:外语被浏览37次2013.05.12 满意答案 vgrwi 采纳率:50%12级 2013.05.13 回车换行 换成十进制就是 #13#10