题意

一串括号序列,只由(和)组成,然后是m个提问,提问l和r区间内,最大的匹配匹配括号数。

思路

第一,贪心的思想,用最正常的方式去尽量匹配,详细点说就是,先找到所有的(),然后删除这些(),再找所有的()。用这种方式匹配出来的,就是最优的方案。

第二,如果我们把所有的'('当成+1, ')'当成-1, 然后画成函数,那么,我们可以通过函数的图像,来得到得到,这段区间的长度(r-l),这段区间的最小值(min),这段区间没用的')'的数量(f(l)-min),这段区间没用的'('的数量(f(r)-f(l)+(f(l)-min)) 然后就得到解了。

另外,区间查询最小值,用线段树就行。

本解 最重要的地方,在于通过函数图像得到解。最难得的就是如果通过函数图像得到解,唉,谁知道呢= =……

我的代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 1020000
#define lson(A) ((A)<<1)
#define rson(A) ((A)<<1|1) int top; char str[N];
int sum[N]; int tree[N<<]; void init(int root, int l, int r) {
if (l == r) {
tree[root] = sum[l];
return;
}
int mid = (l+r)/;
init(lson(root), l, mid);
init(rson(root), mid+, r);
tree[root] = min(tree[lson(root)], tree[rson(root)]);
} int query(int root, int rl, int rr, int ql, int qr) {
if (root == -) exit(-);
//printf("%d, %d %d %d %d\n", root, rl, rr, ql ,qr);
if ((rl == ql && rr == qr) || rl==rr) {
return tree[root];
}
int mid = (rl+rr)/;
if (mid < ql) return query(rson(root), mid+, rr, ql, qr);
if (qr <= mid) return query(lson(root), rl, mid, ql, qr);
else if (ql <= mid && mid+ <= qr)
return min( query(lson(root), rl, mid, ql, min(mid, qr))
,query(rson(root), mid+, rr, max(mid+, ql), qr));
else exit(-);
} int main() {
scanf("%s", str);
sum[] = ;
int i;
for (i = ; str[i]; i++) {
sum[i+] = sum[i] + (str[i] == '(' ? : - );
}
int len = i;
init(, , len); int m;
scanf("%d", &m);
while (m--) {
int l, r;
scanf("%d%d", &l, &r);
l--;
int minnum = query(, , len, l, r);
int skipClose = sum[l] - minnum;
int skipOpen = sum[r]-sum[l]+skipClose;
//printf("(%d, %d,%d)\n", minnum, skipClose, skipOpen);
printf("%d\n", r-l-skipClose-skipOpen);
}
return ;
}

CodeForces 380.C Sereja and Brackets的更多相关文章

  1. Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并

    题目链接:http://codeforces.com/contest/381/problem/E  E. Sereja and Brackets time limit per test 1 secon ...

  2. Sereja and Brackets CodeForces - 380C (树状数组+离线)

    Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...

  3. Sereja and Brackets CodeForces - 380C (线段树+分治思路)

    Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...

  4. CF380C. Sereja and Brackets[线段树 区间合并]

    C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. codeforces 629C Famil Door and Brackets (dp + 枚举)

    题目链接: codeforces 629C Famil Door and Brackets 题目描述: 给出完整的括号序列长度n,现在给出一个序列s长度为m.枚举串p,q,使得p+s+q是合法的括号串 ...

  6. CodeForces 380C Sereja and Brackets(扫描线+树状数组)

    [题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括 ...

  7. 【39.29%】【codeforces 552E】Vanya and Brackets

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. Codeforces 380 简要题解

    ABC见上一篇. 感觉这场比赛很有数学气息. D: 显然必须要贴着之前的人坐下. 首先考虑没有限制的方案数.就是2n - 1(我们把1固定,其他的都只有两种方案,放完后长度为n) 我们发现对于一个限制 ...

  9. Codeforces 314 E. Sereja and Squares

    http://codeforces.com/contest/314/problem/E 题意: 原本有一个合法的括号序列 擦去了所有的右括号和部分左括号 问有多少种填括号的方式使他仍然是合法的括号序列 ...

随机推荐

  1. [vijos]P1979 NOIP2015 信息传递

    描述 有 n 个同学(编号为 1 到 n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i 的同学的信息传递对象是编号为 TiTi 的同学. 游戏开始时,每人都只知道 ...

  2. 设置mysql允许外部连接访问

    错误信息: SQL Error (1130): Host ‘192.168.1.88’ is not allowed to connect to this MySQL server 说明所连接的用户帐 ...

  3. matplotlib学习记录 一

    from matplotlib import pyplot as plt # 先实例一个图片,传入图片参数,10宽,5高,分辨率为80 image = plt.figure(figsize=(10,5 ...

  4. 剑指Offer(书):顺时针打印数组

    题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1 ...

  5. UVa - 1593 代码对齐(STL)

    看上去十分麻烦的一道题,但是看了看别人的写法感觉大神们写的无比简单. 就是记一个每列单词的最大长度,然后剩下的事交给NB的iomanip头文件就好. stringsteam是一个神奇的东西. #inc ...

  6. ACM-ICPC 2018 徐州赛区网络预赛 B. BE, GE or NE

    In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named &qu ...

  7. ACM-ICPC 2017 Asia Urumqi A. Coins

    Alice and Bob are playing a simple game. They line up a row of n identical coins, all with the heads ...

  8. ModelViewSet的继承关系

  9. luogu3369 【模板】普通平衡树(Treap/SBT) treap splay

    treap做法,参考hzwer的博客 #include <iostream> #include <cstdlib> #include <cstdio> using ...

  10. 自建NAS如何使用大于2TB的硬盘(从分区开始)

    目录 自建NAS如何使用大于2TB的硬盘(从分区开始) 对分区进行格式化 挂载到某一目录(需设置开机自动挂载) 上传文件测试: 补充 自建NAS如何使用大于2TB的硬盘(从分区开始) 需求说明: 自建 ...