题目链接

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. Mybatis的小技巧

    一.使用resultMap字段关联对象属性太麻烦 eg:过于复杂,类似这种结果集转换的,只需要在配置文件中开启自动转换进行了,无需再手动写了很麻烦 替换办法:开启骆驼命名法进行匹配就ok了,实体类字段 ...

  2. 华为开发者联盟 方舟编译器 DevEco IDE

    华为开发者联盟 https://developer.huawei.com/consumer/cn/ 方舟编译器是为支持多种编程语言.多种芯片平台的联合编译.运行而设计的统一编程平台,包含编译器.工具链 ...

  3. kotlin之变量与常量

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/217 kotlin之变量与常量 最近开始做新产品,于是乎用 ...

  4. CentOS7 安装Python3.6.8

    CentOS7 安装Python3.6.8 1. 安装依赖环境 yum -y groupinstall "Development tools" yum -y install zli ...

  5. Win10锁屏壁纸位置

    C:\Users\MIS\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalStat ...

  6. MySQL常用DDL、DML、DCL语言整理

    DDL ----Data Definition Language 数据库定义语言 如 create procedure之类 创建数据库 CREATE DATABASE [IF NOT EXISTS] ...

  7. win10+Ubuntu16.04双系统下深度学习环境的搭建

    环境零零碎碎地搭了三四天,虽然碰到各种问题,但还是搭建好了,自己整理记录下,同时也算给有需要的人一些指导吧 一.双系统的安装 Win10硬盘管理助手 压缩或者直接利用未使用的空间,空间大小自定,将腾出 ...

  8. 小程序之--动态设置页面标题 wx.setNavigationBarTitle

    参考地址 http://www.yilingsj.com/xwzj/2018-11-26/weixin-navigationbartitletext.html 页面最初是[在线教研] 可以在这个页面的 ...

  9. opencv加载图片imread失败的原因

    用简单的imshow函数加载图片,报加载失败的异常,显示没有将图片加载到内存中.原因是在配置环境是同时将*lib与*d.lib都入了附加依赖项,而项目的生成方式选择的是debug,*lib在*d.li ...

  10. SQL查询语法30例

    学好SQL查询:无他,概手熟耳. 1. 基础表: 学生表: 老师表: 课程表: 成绩表: 2. 题目: 1.查询名字中含有"华"字的学生信息 select * from 学生 wh ...