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 ...
随机推荐
- B. Light bulbs
B. Light bulbs There are NNN light bulbs indexed from 000 to N−1N-1N−1. Initially, all of them are o ...
- 使用EA画流程图
https://www.sparxsystems.com.au/enterprise_architect_user_guide/13.0/model_domains/flow_chart.html
- flutter样式基础
设置padding 1. 可以使用 Padding类设置 Padding( padding: const EdgeInsets.all(8.0), child:, ); 2. Container 参数 ...
- 同源策略、跨域、json和jsonp
同源策略 源(origin)就是协议.域名和端口号.若地址里面的协议.域名和端口号均相同则属于同源. 以下是相对于 http://www.a.com/test/index.html 的同源检测 • h ...
- 虚拟机字节码指令表 JVM
虚拟机字节码指令表 标签(空格分隔): Java基础 JVM 记录虚拟机字节码指令,方便分析.以下内容来自<深入理解Java虚拟机> 字节码 助记符 指令含义 0x00 nop 什么都不做 ...
- 082、Java数组之数组传递之简化理解
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 算法:辗转相除法求最大公约数(C语言实现)
辗转相除法,一种求最大公约数的算法 已知:A / B = C ······ R (A.B.C.R皆是整数) 假设:D是A的余数,D也是B的余数,那么D就是A和B的公约数 D是A和B的约数,则A和B是 ...
- LR_问题_平均响应时间解释,summary与analysis不一致----Summary Report中的时间说明
Summary是按整个场景的时间来做平均的,最大最小值,也是从整个场景中取出来的. (1) 平均响应时间:事物全部响应时间做平均计算 (2) 90%响应时间:将事物全部响应时间 ...
- 新手小白如何向GitHub上提交项目
首先你得注册一个自己的GitHub账号,注册网址:https://github.com/join 创建一个新的项目,填写项目名称,描述 创建完成之后,跳转到下面的页面,下面红框中的网址要记住,在后面上 ...
- elasticsearch-java客户端测试
1.环境准备 (1)添加依赖 <dependency> <groupId>org.elasticsearch.client</groupId> <artifa ...