题目链接

problem

给出一个括号序列,要求删除一些括号使得剩下的括号序列是个匹配的括号序列,且改括号序列左边全部为左括号,右边全部为右括号。

solution

考虑枚举左右括号交界的位置\(x\),为了避免重复计算,强制要求\(x\)左边的第一个左括号必选。然后枚举\(x\)的时候只枚举左括号的位置。

然后枚举括号序列的长度。假设长度为\(2i\),那么左右括号就分别有\(i\)个,假设左边有\(n\)个左括号,右边有\(m\)个右括号。那么该位置的答案就是\(\sum\limits_{i=1}^{min(n,m)}C_{n-1}^{i-1}C_{m}^i\)

观察上面这个式子,当\(i=0\)时没有贡献,所以我们可以等价的写成\(\sum\limits_{i=0}^{min(n,m)}C_{n-1}^{i-1}C_m^i\)

假设\(n\le m\)

上面的式子也可以写成

\(\sum\limits_{i=0}^nC_{n-1}^{n-i}C_m^i\)

考虑这个东西的组合意义,也就相当于有\(n+m-1\)个物品从中选\(n\)个。

所以上面的东西其实就是\(C_{n+m-1}^n\)然后就可以\(O(1)\)求了。

可以发现,当\(m\le n\)时,推出的式子也是这个。

这样总复杂度就成了\(O(n)\)

code

#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<ctime>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
ll read() {
ll x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;c = getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
int n;
const int N = 200100;
char s[N];
int cnta,cntb,jc[N],inv[N];
int C(int x,int y) {
return 1ll * jc[x] * inv[y] % mod * inv[x - y] % mod;
}
int qm(int x,int y) {
int ret = 1;
for(;y;y >>= 1,x = 1ll * x * x % mod) {
if(y & 1) ret = 1ll * ret * x % mod;
}
return ret;
} int main() {
scanf("%s",s + 1);
n = strlen(s + 1);
jc[0] = 1;
for(int i = 1;i <= n;++i) jc[i] = 1ll * jc[i - 1] * i % mod;
for(int i = 0;i <= n;++i) inv[i] = qm(jc[i],mod - 2);
for(int i = 1;i <= n;++i) if(s[i] == ')') cntb++;
ll ans = 0;
for(int i = 1;i <= n;++i) {
if(s[i] == '(') {
cnta++;
ans += C(cnta + cntb - 1,cnta);
ans %= mod;
}
else cntb--;
}
cout<<ans; return 0;
}

CF785D Anton and School - 2的更多相关文章

  1. CF785D Anton and School - 2 解题报告

    CF785D Anton and School - 2 题意:给定一个长度\(\le 2 \times 10e5\)由'('和')'组成的字符串,问有多少个子串(可以不连续),前半部分是由\('('\ ...

  2. CF785D Anton and School – 2

  3. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  4. 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know

    题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...

  5. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径

    E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...

  6. Codeforces Round #379 (Div. 2) D. Anton and Chess 水题

    D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...

  7. Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分

    C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...

  8. Codeforces Round #379 (Div. 2) B. Anton and Digits 水题

    B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...

  9. Codeforces Round #379 (Div. 2) A. Anton and Danik 水题

    A. Anton and Danik 题目连接: http://codeforces.com/contest/734/problem/A Description Anton likes to play ...

随机推荐

  1. C lang:The smallest negative plus one equals the largest positive

  2. RHEL5.6静默安装oracle11.2.0数据库实例脚本

    脚本:单实例静默安装echo '[GENERAL] RESPONSEFILE_VERSION = "11.2.0" //查看虚拟机的版本,不能更改 OPERATION_TYPE = ...

  3. ORA-17627: ORA-12577:关于文件存储满的问题

    问题描述:搭建DG的时候,要rman从orcl恢复到orclstd数据库来,dup复制了半天,结果最后报错:ORA-17627: ORA-12577: Message 12577 not found; ...

  4. MySQL数据库~~~~~锁和事务

    一 锁 innodb存储引擎默认是行级锁 行级锁定最大的特点就是锁定对象的颗粒度很小,也是目前各大数据库管理软件所实现的锁定颗粒度最小的.由于锁定颗粒度很小,所以发生锁定资源争用的概率也最小,能够给予 ...

  5. 表单生成器(Form Builder)之mongodb表单数据查询——返回分页数据和总条数

    上一篇笔记将开始定义的存储结构处理了一下,将FormItems数组中的表单项都拿到mongodb document的最外层,和以前的关系型数据类似,之不过好多列都是动态的,不固定,不过这并没有什么影响 ...

  6. [PHP] Workerman中的注册树模式

    注册树模式是把对象挂到一个类的属性数组里,下次直接在这个数组里面取,保持全局唯一,一般在项目入口初始化的时候有用到.在workerman中一开始的就是个注册树模式的运用,下面是对他的模拟 <?p ...

  7. Mysql基本注入

    实验环境:墨者学院Mysql手工注入漏洞测试靶场 后台源码没有进行任何字符过滤. 首先进入靶场环境 先用admin登陆试试 果然不行,这时看到用户登录下方有一个停机维护通知,点进去瞅瞅 看到这里链接上 ...

  8. JavaScript定时器方法

    一.setTimeout() 延迟性操作 window.setTimeout(function(){ console.log('派大星');//延迟了4秒 },4000); console.log(' ...

  9. go语言变量作用域

    Go 语言变量作用域 作用域为已声明标识符所表示的常量.类型.变量.函数或包在源代码中的作用范围. Go 语言中变量可以在三个地方声明: 函数内定义的变量称为局部变量 函数外定义的变量称为全局变量 函 ...

  10. Sql sever DateDiff 函数

    函数: DATEDIFF(datepart,startdate,enddate) 具体实例: --相差年数 结果0 SELECT DATEDIFF(yy,'2008-12-29','2008-12-1 ...