传送门

题意:

  给你一个只包含 '(' 和 ')' 的长度为 n 字符序列s;

  给出一个操作:将第 i 个位置的字符反转('(' ')' 互换);

  问有多少位置反转后,可以使得字符串 s 变为"Regular Bracket Sequence";

  输出满足条件的位置的个数;

题解:

  令 '(' = 1 , ')' = -1;

  定义 sum[i]:括号序列的前缀和;

  一个合法的括号匹配串的充要条件是:

    [1] 对于任何 i,sum[i] ≥ 0;

    [2] sum[n]=0;

 int n;
char s[maxn];
int sum[maxn];///前缀和
int a[maxn];///a[i]:min{sum[1,..,i]}
int b[maxn];///b[i]:min{sum[i,..,n]}

  枚举位置 i,判断将位置 i 反转后序列是否变为 "Regular Bracket Sequence";

 ///i位置反转只会影响[i,n]的sum值
///首先要确保[1,i-1]序列满足条件[1]
///接着判断将i位置反转后
///①sum[n]±2是否为0
///②b[i]±2是否≥0
bool isSat(int i)///判断i位置反转后序列是否可以变为RBS
{
if(a[i-] < )
return false;
if(s[i] == ')')
return b[i]+ >= && sum[n]+ == ? true:false;
else
return b[i]- >= && sum[n]- == ? true:false;
}

AC代码:

 #include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e6+; int n;
char s[maxn];
int sum[maxn];
int a[maxn];
int b[maxn]; ///i位置反转只会影响[i,n]的sum值
///首先要确保[1,i-1]序列满足条件[1]
///接着判断将i位置反转后
///①sum[n]±2是否为0
///②b[i]±2是否≥0
bool isSat(int i)///判断i位置反转后序列是否可以变为RBS
{
if(a[i-] < )///[1,i-1]不满足条件[1]
return false;
if(s[i] == '(')///'('变为')' i及其之后的sum-2
return b[i]- >= && sum[n]- == ? true:false;
else
return b[i]+ >= && sum[n]+ == ? true:false;
}
int Solve()
{
sum[]=;
for(int i=;i <= n;++i)
sum[i]=sum[i-]+(s[i] == '(' ? :-); a[]=INF;
for(int i=;i <= n;++i)
a[i]=min(sum[i],a[i-]);
b[n+]=INF;
for(int i=n;i >= ;--i)
b[i]=min(sum[i],b[i+]); int ans=;
for(int i=;i <= n;++i)
if(isSat(i))
ans++; return ans;
}
int main()
{
scanf("%d%s",&n,s+);
printf("%d\n",Solve()); return ;
}

Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence(思维)的更多相关文章

  1. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维,模拟栈)

    题意:给你一串括号,每次仅可以修改一个位置,问有多少位置仅修改一次后所有括号合法. 题解:我们用栈来将这串括号进行匹配,每成功匹配一对就将它们消去,因为题目要求仅修改一处使得所有括号合法,所以栈中最后 ...

  2. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

    Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...

  3. Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈

    C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...

  4. Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence

    题目链接:http://codeforces.com/contest/612/problem/C 解题思路: 题意就是要求判断这个序列是否为RBS,每个开都要有一个和它对应的关,如:<()> ...

  5. # Codeforces Round #529(Div.3)个人题解

    Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...

  6. CodeForces Round #529 Div.3

    http://codeforces.com/contest/1095 A. Repeating Cipher #include <bits/stdc++.h> using namespac ...

  7. Codeforces Round #529 (Div. 3) 题解

    生病康复中,心情很不好,下午回苏州. 刷了一套题散散心,Div 3,全部是 1 A,感觉比以前慢了好多好多啊. 这几天也整理了一下自己要做的事情,工作上要努力... ... 晚上还是要认认真真背英语的 ...

  8. Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)

    Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...

  9. Codeforces Round #529 (Div. 3) C. Powers Of Two

    http://codeforces.com/contest/1095/problem/C 题意:给n找出k个2的幂,加起来正好等于n.例如 9,4:9 = 1 + 2 + 2 + 4 思路:首先任何数 ...

随机推荐

  1. 【滴水石穿】rn

    这个项目还不错,还比较全 先放项目地址:https://github.com/ShionHXC/rn 项目算是一个完整的APP 有用到redux-thunk存储数据,算的上是一个普通的比较完整的APP ...

  2. Codeforces 436C

    题目链接 C. Dungeons and Candies time limit per test 2 seconds memory limit per test 256 megabytes input ...

  3. LintCode_408 二进制求和

    给定两个二进制字符串,返回他们的和(用二进制表示). 思路 string s = ""; 目标字符串 cp 存储进位;取 0或1 sum = a[i] + b[i] + cp;分为 ...

  4. phonegap支付宝2.0移动快捷支付插件IOS版

    坑爹的支付宝,一两年都没有更新sdk了,这两天突然更新sdk,而且更新的变化特别大,所以只能对之前的支付宝快捷支付插件重新写了一遍. 这样既顺应了支付宝的更新,同时也支持了ios8. 废话少说,集成过 ...

  5. Nginx 外的另一选择,轻量级开源 Web 服务器 Tengine 发布新版本

    新版发布 近日,轻量级开源 Web 服务器 Tengine 发布了2.3.0版本,新增如下特性: ngx_http_proxy_connect_module,该模块让 Tengine 可以用于正向代理 ...

  6. 获取登录的地点和ip地址的js

    <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> <script>doc ...

  7. jupyter的简单操作

    jupyter简单使用 esc+ m 切换到标记模式 shift + enter 运行 a 向上新增代码块 b 向下新增代码块 dd 删除代码块 y python代码模式 file --- downl ...

  8. 2019-4-10-VisualStudio-2019-尝试使用-C#-8.0-新的方式

    title author date CreateTime categories VisualStudio 2019 尝试使用 C# 8.0 新的方式 lindexi 2019-04-10 10:41: ...

  9. java连接oracle jdbc连接

    Class.forName("oracle.jdbc.driver.OracleDriver"); Connection ct=Driver.Magager.getConnecti ...

  10. constexpr:编译期与运行期之间的神秘关键字

    Scott Meyers在effective modern c++中提到“If there were an award for the most confusing new word in C++11 ...