【乱搞】【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 ...
随机推荐
- 怎样安装TortoiseGit
TortoiseGit是基于Windows的Git图形化工具 访问 https://tortoisegit.org/
- 常用SQL语句大全(SQL Server)
一.基础 查看数据库状态 select state_desc from sys.databases where name='dbname' -- dbname数据库名 1.说明:创建数据 ...
- javascript提高篇
本章简介 本章内容比较少,有三个分享的知识.你可能都看过了,因为网上也有很多提问和解答,如果没看过或者没搞懂,你可以再看看这篇文章. 1. 数组去重方法的演变 -- 走向代码缩短化 2. [] ...
- input value="值栈的值"
<input type="text" value="<s:property value="myp.begintime"/>" ...
- ios framework 使用图片资源
framework 的制作工程见:http://www.cocoachina.com/ios/20141126/10322.html: 遇到问题: 由于自己的framework 要使用图片资源,最后找 ...
- 【Leetcode】113Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- Web服务器负载均衡的几种方案 : DNS轮询
本篇主要讲一下最简单的方案——DNS轮询. DNS轮询 大多域名注册商都支持多条A记录 的解析,其实这就是DNS轮询 ,DNS 服务器 将解析请求按照A记录 的顺序,逐一分配到不同的IP上,这样就完成 ...
- 【C++】C++ static关键字详解
static的作用 1.隐藏 当我们编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性,其他的源文件也能访问.如,我们有源文件source1.cpp定义了一个全局变量i和函数Fu ...
- python的N个小功能(文件内容的匹配替换)
# -*- coding: utf-8 -*- """ Created on Fri Feb 17 20:25:05 2017 @author: who "&q ...
- 【bzoj4244】邮戳拉力赛 背包dp
题目描述 IOI铁路是由N+2个站点构成的直线线路.这条线路的车站从某一端的车站开始顺次标号为0...N+1. 这条路线上行驶的电车分为上行电车和下行电车两种,上行电车沿编号增大方向行驶,下行电车沿编 ...