Description

给定一个长度为 \(n\) 的小括号序列,求有多少个位置满足将这个位置的括号方向反过来后使得新序列是一个合法的括号序列。即在任意一个位置前缀左括号的个数不少于前缀右括号的个数,同时整个序列左右括号个数相同

Input

第一行是一个整数 \(n\) 代表序列长度

下面一行是括号序列

Output

输出一行一个数字代表的答案

Hint

\(1~\leq~n~\leq~10^6\)

Solution

考虑一个位置,如果是左括号,那么能将其变成右括号当且仅当:

整个序列左括号个数比右括号多 \(2\)

在这个位置之前,所有位置的前缀左括号个数都不少于前缀右括号个数

在这个位置和这个位置之后,在修改后所有位置的前缀左括号个数都不少于前缀右括号个数

我们将第三条转化一下,由于在修改之后左括号个数减一,右括号个数加一,于是我们可以将第三条改为

在这个位置和这个位置之后,修改前所有位置的前缀左括号个数都比前缀右括号个数至少多两个

这样一个位置能不能修改就仅与原序列有关了。

若果是右括号,也同理进行分析,将两个改为 \(-2\) 个即可。

于是我们处理一下前缀左括号个数-右括号个数。

考虑第二条限制,等价于 \([1,pos]\) 这些前缀中的最小值不小于 \(0\)。于是我们对这些前缀求一个前缀最小值,就可以做到 \(O(1)\) 判断了。

对于第三条限制,等价于 \([pos,n]\) 这些前缀中的最小值不小于 \(2\)。于是我们对这些前缀求一个后缀最小值,也可以做到 \(O(1)\) 判断了。

这个位置是右括号的情况同理。细节参考代码

Code

#include <cstdio>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long typedef long long int ll; namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
} template <typename T>
inline void qr(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
} template <typename T>
inline void ReadDb(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
} namespace OPT {
char buf[120];
} template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
rg int top=0;
do {OPT::buf[++top] = x % 10 + '0';} while (x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
} const int maxn = 1000010; int n, ans;
int cnt[maxn], pre[maxn], post[maxn];
char MU[maxn]; int main() {
freopen("1.in", "r", stdin);
qr(n);
for (rg int i = 1; i <= n; ++i) {
do {MU[i] = IPT::GetChar();} while ((MU[i] != '(') && (MU[i] != ')'));
if (MU[i] == '(') cnt[i] = cnt[i - 1] + 1;
else cnt[i] = cnt[i - 1] - 1;
}
if ((cnt[n] != 2) && (cnt[n] != -2)) return puts("0") & 1;
pre[0] = maxn; post[n + 1] = maxn;
for (rg int i = 1; i <= n; ++i) pre[i] = std::min(pre[i - 1], cnt[i]);
for (rg int i = n; i; --i) post[i] = std::min(post[i + 1], cnt[i]);
for (rg int i = 1; i <= n; ++i) {
if (MU[i] == '(') {
if ((cnt[n] == 2) && (post[i] >= 2) && (pre[i - 1] >= 0)) ++ans;
} else {
if ((cnt[n] == -2) && (post[i] >= -2) && (pre[i - 1] >= 0)) ++ans;
}
}
qw(ans, '\n', true);
return 0;
}

【乱搞】【CF1095E】 Almost Regular Bracket Sequence的更多相关文章

  1. CF1095E Almost Regular Bracket Sequence

    题目地址:CF1095E Almost Regular Bracket Sequence 真的是尬,Div.3都没AK,难受QWQ 就死在这道水题上(水题都切不了,我太菜了) 看了题解,发现题解有错, ...

  2. E. Almost Regular Bracket Sequence 解析(思維)

    Codeforce 1095 E. Almost Regular Bracket Sequence 解析(思維) 今天我們來看看CF1095E 題目連結 題目 給你一個括號序列,求有幾個字元改括號方向 ...

  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. Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

    C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  5. Replace To Make Regular Bracket Sequence

    Replace To Make Regular Bracket Sequence You are given string s consists of opening and closing brac ...

  6. D - Replace To Make Regular Bracket Sequence

    You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). ...

  7. CodeForces - 612C Replace To Make Regular Bracket Sequence 压栈

    C. Replace To Make Regular Bracket Sequence time limit per test 1 second memory limit per test 256 m ...

  8. (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)

    (CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...

  9. 贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

    题目传送门 /* 题意:求最长括号匹配的长度和它的个数 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 详细解释:http://bl ...

  10. Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)

    Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...

随机推荐

  1. PHPCMS如何让手机站点取消浏览大图直接加载原图

    一.然后找到phpcms\modules\wap\functions\global.func.php 文件,找到相关代码,如下图: return '<img src="'.thumb( ...

  2. jupyter通过notedown使用markdown

    0 Problem 最近看了下李沐老师的mxnet教程,在使用jupyter的时候打开教程发现全是markdown源文,没有展示markdown格式的文字. 1 Reason 源代码是用markdow ...

  3. PowerDesigne 建立概念数据模型

    本文主要介绍PowerDesigner概念数据模型以及实体.属性创建. 一.新建概念数据模型1)选择File-->New,弹出如图所示对话框,选择CDM模型(即概念数据模型)建立模型. 2)完成 ...

  4. /proc/sys目录下各文件参数说明

    linux 其他知识目录 原文链接:https://blog.csdn.net/hshl1214/article/details/4596583 一.前言本文档针对OOP8生产环境,具体优化策略需要根 ...

  5. centos下安装升级python到python3.5

    本文摘抄自:https://www.cnblogs.com/edward2013/p/5289056.html  请支持原版 CentOS7安装Python3.5   2. 安装Python的依赖包 ...

  6. 为phpStorm 配置PHP_CodeSniffer自动检查代码

    通过composer 安装PHP_CodeSniffer : squizlabs/PHP_CodeSniffer gihub地址 composer global require "squiz ...

  7. 02慕课网《进击Node.js基础(一)》——CommonJs标准

    是一套规范管理模块 每个js 为一个模块,多个模块作为一个包 node.js和Couchdb是对其的实现: 不同于jQuery 模块:定义.标识.引用(地址/模块名称) 模块类型: 核心模块http ...

  8. erlang转化中文为url

    今天使用http get 方法时,参量中有中文而导致出错. 例如http://abc.com/abc?arg=中文,在erlang使用http:request方法失败. 后来查了url的规范,url中 ...

  9. docker+mesos+marathon

    前言 (Core) [root@docker-slave ~]# uname -r 3.10.0-229.4.2.el7.x86_64 [root@docker-slave ~]# uname -m ...

  10. Nginx LVS HAProxy 对比

    一般对负载均衡的使用是随着网站规模的提升根据不同的阶段来使用不同的技术.具体的应用需求还得具体分析,如果是中小型的Web应用,比如日PV小于1000万,用Nginx就完全可以了:如果机器不少,可以用D ...