【乱搞】【CF1095E】 Almost Regular Bracket Sequence
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的更多相关文章
- CF1095E Almost Regular Bracket Sequence
题目地址:CF1095E Almost Regular Bracket Sequence 真的是尬,Div.3都没AK,难受QWQ 就死在这道水题上(水题都切不了,我太菜了) 看了题解,发现题解有错, ...
- E. Almost Regular Bracket Sequence 解析(思維)
Codeforce 1095 E. Almost Regular Bracket Sequence 解析(思維) 今天我們來看看CF1095E 題目連結 題目 給你一個括號序列,求有幾個字元改括號方向 ...
- 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 ...
- 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 ...
- Replace To Make Regular Bracket Sequence
Replace To Make Regular Bracket Sequence You are given string s consists of opening and closing brac ...
- D - Replace To Make Regular Bracket Sequence
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). ...
- 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 ...
- (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...
- 贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence
题目传送门 /* 题意:求最长括号匹配的长度和它的个数 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 详细解释:http://bl ...
- Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)
Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...
随机推荐
- 【Jmeter测试】BeanShell介绍和使用
BeanShell是什么? BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法; BeanShell是一种松散类型的脚本语言: BeanShell是用Jav ...
- jmeter接口测试实例:带参数、带token
测试内容简介: 1.get请求,无参数 2.get请求,参数为第一条响应中的id 3.get请求,带token 结构图: 下面进行详解: 一.添加cookie管理器等 1.添加ht ...
- 设置PNG图片DPI 信息,保存为PDF(使用Magick),与OpenCV转换
目录 任务描述 解决方案 Magick++ Talk is cheap, show me the code. 与 Opencv 配合 相关链接 任务描述 我有这样一个需求,读取一张格式为PNG 或者 ...
- 美国警察iPhone数据线挡住歹徒子弹获救
泡泡网手机频道11月1日 现在手机的功能越来越丰富,不仅可以接打电话.收发短信.玩游戏聊天,关键时刻还能救命.前天HTC手机再次忠心护主,让许多同学对HTC赞赏有加.而现在又有人捡了一条命,不过这次救 ...
- Java:有关try、catch和finally的学习(供自己参考)
Java:有关try.catch和finally的学习 在看到书本的时候对finally的介绍是:不论是否在try块中产生异常,都会执行finally.当时对这句话的理解不够深,误以为在try...c ...
- 《C》VS控制台应用
源(c)文件:主要是源码,包括程序入口,函数的实现 头(h)文件:主要是定义的函数声明 资源(rc)文件:程序中用到的辅助资源,比如位图,图标资源 解决VS2015安装后stdio.h ucrtd.l ...
- centos7环境下mysql安装
1.去官网下载合适的yum源安装包 https://dev.mysql.com/downloads/repo/yum/ 2.yum 本地安装 命令:yum localinstall mysql57-c ...
- Croc Champ 2013 - Round 1 E. Copying Data 线段树
题目链接: http://codeforces.com/problemset/problem/292/E E. Copying Data time limit per test2 secondsmem ...
- HDU 5672 String
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5672 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- windows+ubuntu时间修改问题
只需要在ubuntu系统数输入一行指令即可 timedatectl set-local-rtc 1 --adjust-system-clock