D

给你一个长度为n的括号序列,然后你可以选择交换两个位置,你需要使得能够变成 合法括号序列的起点最多。

题解

  1. 人尽皆知的东西:合法的括号序列是,令'('为1,')'为-1,那么前缀和需要>=0,且最后的总和应该为0.

  2. 假设现在已经是交换好的序列了,那么答案个数,就是前缀和的最小值的个数。这是因为最小值,例如最小值-1(或者-2),造成这个-1(或者-2)原因就是前面的没有一个1(或者2),那我们就得把前面的-1-2放到后面去,意思就是将最小值的后面第一段作为起点

  3. 如果交换'('和')',会使得这个区间的每个数的前缀和-=2,不包括交换点。如果交换')'和'(',你可以改变交换为a[i]和a[j+n],将其变成交换'('和')'

  4. 因为我们使得这个区间里面的每个数都减去了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的更多相关文章

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

  2. Codeforces Round #594 (Div. 2)

    传送门 C. Ivan the Fool and the Probability Theory 题意: 给出一个\(n*m\)的方格,现在要给方格中的元素黑白染色,要求任一颜色最多有一个颜色相同的格子 ...

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

  4. Codeforces Round #594 (Div. 1) C. Queue in the Train 模拟

    C. Queue in the Train There are

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

  6. Codeforces Round #594 (Div. 2) B. Grow The Tree 水题

    B. Grow The Tree Gardener Alexey teaches competitive programming to high school students. To congrat ...

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

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

  9. Codeforces Round #594 (Div. 1)

    Preface 这场CF真是细节多的爆炸,B,C,F都是大细节题,每道题都写了好久的说 CSP前的打的最后一场比赛了吧,瞬间凉意满满 希望CSP可以狗住冬令营啊(再狗不住真没了) A. Ivan th ...

随机推荐

  1. Lesson 13 The search for oil

    What do oilmen want to achieve as soon as they strike oil? The deepest holes of all are made for oil ...

  2. LibreOJ #6001. 「网络流 24 题」太空飞行计划

    \(\quad\) 与网络流有关的最值有三个:最大流,最小费用,最小割.这道题是最小割.想了好久,终于想明白最小割应该怎么用. \(\quad\) 先找出矛盾的事物.在这道题中,两件事是矛盾的:做实验 ...

  3. SessionAttributes注解

    SessionAttributes注解: a.该注解只能应用在类上: b.该注解用于将Map.ModelMap.Model或ModelAndView中的数据暂存到HttpSession中以使其可以在多 ...

  4. 十七 Spring的JDBC模版:使用开源连接池,DBCP,C3P0

    DBCP的配置以及使用 引入jar包

  5. C#往TextBox的方法AppendText加入的内容里插入换行符----转载

    C# TextBox换行[huan hang]时你往往会想到直接付给一个含有换行[huan hang]符"\n"的字符[zi fu]串[zi fu chuan]给Text属性[sh ...

  6. BEC合约整数溢出漏洞还原与分析

    一.币圈一秒,人间一年 有道是币圈一日,人间一年.这个说法又得升级了,叫币圈一秒,人间一年. 前不久,币圈又出大事啦.BEC智能合约被爆出整数溢出漏洞,导致黑客能无限印币,在一次交易中,也就那么几秒钟 ...

  7. 实时监听input输入的变化(兼容主流浏览器)

    遇到如此需求,首先想到的是change事件,但用过change的都知道只有在input失去焦点时才会触发,并不能满足实时监测的需求,比如监测用户输入字符数. 在经过查阅一番资料后,欣慰的发现firef ...

  8. CF6

    A A 不解释 #include<bits/stdc++.h> using namespace std; namespace red{ inline int read() { int x= ...

  9. 「CF10D」LCIS

    传送门 Luogu 解题思路 首先考虑怎么求方案,这样才可能会输出方案. 考虑 \(\text{DP}\). 设 \(f[i][j]\) 表示在 \(a\) 序列中选择一个 \([1...i]\) 的 ...

  10. clientDataSet转换sql

    ReadMe 新版本delphi,可以用string类型,旧版本需要用widestring =========================================== function T ...