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. 题解 P4949 【最短距离】

    吼题啊 刚开始看上去又以为是LCT啥子的. 后来发现,TM是个图. 然后果断准备放弃,突然发现只有N个点N条边. woc,这不就一个基环树上树链剖分吗... 关于基环树问题,相信大家都一定很有经验了吧 ...

  2. Python 中的else

    在其他程序语言中,else 似乎只是与 if 关键字有缘分.而与其他的关键字没有联系,不能搭配使用,而在python中,else 除了与 if 匹配外, 还可以与for.while/ try等关键字匹 ...

  3. 新见Java数据类型_需了解

    LinkedList<T>.LinkedList.poll() 先给出结论:pop 与 poll 都是取出 LinkedList 的第一个元素,并将该元素删除,等效于:removeFirs ...

  4. 十三、web应用中路径总结

    1.路径的编写形式:1)<a href="/项目名/资源路径"></a>    <!--超链接--> <form action=" ...

  5. java虚拟机之垃圾回收机制

    一.需要回收的内存区域     程序计数器.虚拟机栈.本地方法栈 3 个区域随线程生灭(因为是线程私有),栈中的栈帧随着方法的进入和退出而有条不紊地执行着出栈和入栈操作.而 Java 堆和方法区则不一 ...

  6. n/20 文本框动态监听输入长度

  7. PCS 7 V9.0 SP1安装过程截图

  8. Springboot配置文件内容加密

      使用的是jasypt-spring-boot-starter,具体介绍可以参考 https://gitee.com/yangziyi2017/Jasypt-Spring-Boot   引入依赖 & ...

  9. linux安装软件的几种方法----linux下编译安装软件的一般步骤

    linux安装软件的几种方法: 一.rpm包安装方式步骤: 1.找到相应的软件包,比如soft.version.rpm,下载到本机某个目录: 2.打开一个终端,su -成root用户: 3.cd so ...

  10. Windows驱动开发-r3和r0通信

    用户部分代码: int main() { HANDLE hDevice = CreateFile(L, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ...