B. Dispersed parentheses 记忆化搜索 + 括号序列的状压表示
http://codeforces.com/gym/100633/problem/B
2 seconds
256 megabytes
standard input
standard output
The sequence of calculations in arithmetic expressions is usually set by a certain arrangement of parentheses. For example, (3·(2 + 1))·(4 - 5). After deleting all the elements from the expression except parentheses remaining symbols form a parentheses sequence (())(). Let’s assume that adding character «0» does not corrupt the sequence. Let’s call such sequence a disperse parentheses sequence. Also this can be defined as follows:
- An empty line is a disperse parentheses sequence.
- If S and T — disperse parentheses sequences, then lines 0S, S0, (S) and ST are also disperse parentheses sequences.
The depth of disperse parentheses sequence is the maximum difference between the number of opening and closing parentheses in the sequence prefix. (The prefix of line S is the line, which can be obtained from S by deleting symbols from the tail of the line. For example, the prefixes of line «ABCAB» are lines «», «A», «AB», «ABC», «ABCA» and «ABCAB».) Thus, the depth of the sequence «(0)(0())0» equals two (prefix «(0)(0(» contains three openinig and one closing parentheses).
Calculate the number of possible disperse parentheses sequences n symbols long, that have a depth k.
Single line contains space-separated integers n and k (1 ≤ n ≤ 300, 0 ≤ k ≤ n).
Output the number of possible disperse parentheses sequences n symbols long, that have a depth k modulo (109 + 9).
3 0
1
3 1
3
3 2
0 这里学到了一个括号序列的状压表示,也就是,要表示一个合法的括号序列,除了用[lef][rig]表示有开括号lef个,
闭括号rig个之外,还可以用她们的差来表示,直接压缩到一维,[dis]表示两种括号的差值。可知当rig > lef的时候,整个序列是不可能合法的。所以dis < 0直接是0.
那么设dp[n + 1][dis][k]表示长度是n的括号序列,差值是dis,深度是k的合法情况。
ans = dp[n + 1][0][k] 那么可以按位dfs,开始的状态是dis = 0, deep = 0, lef = 0, rig = 0,那么在第cur位,你可以放0,可以放'('或者')'。然后三种情况统计一次的和,就是第cur位的合法情况。 一开始从dp[1][0][0]出发,推导dp[n + 1][0][k]
dp[1][0][0]的意思是在第0位,差值是0,深度是k的合法情况数。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset> int n, k;
const int MOD = 1e9 + ;
const int maxn = + ;
bool vis[maxn][maxn][maxn];
int dp[maxn][maxn][maxn];
int dfs(int cur, int dis, int deep, int lef, int rig) {
if (dis < || deep > k) return ;
if (vis[cur][dis][deep]) return dp[cur][dis][deep];
vis[cur][dis][deep] = true;
if (cur == n + ) {
if (dis == && deep == k) {
return dp[cur][dis][deep] = ;
} else return ;
}
LL ans = ;
ans += dfs(cur + , dis, deep, lef, rig);
ans += dfs(cur + , dis + , max(deep, lef + - rig), lef + , rig);
ans += dfs(cur + , dis - , max(deep, rig + - lef), lef, rig + );
return dp[cur][dis][deep] = ans % MOD;
}
void work() {
scanf("%d%d", &n, &k);
cout << dfs(, , , , ) << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
如果不用这个括号序列的状压,则MLE。dp[n][deep][lef][rig]
2017年7月17日 20:40:26
现在来看这题。
其实是从第一位开始构造。
dp[i][j][k]
表示构造了i个,左括号和右括号差值是j,现在深度是k,在这个状态下,后面那些序列随便变,最后得到的合法情况数会是dp[i][j][k]
整个dfs的过程相当于在第cur位放什么的过程。
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; const int MOD = 1e9 + ;
LL dp[][][];
bool vis[][][];
int n, k;
LL dfs(int cur, int dis, int depth) {
if (depth > k) return ;
if (cur == n + ) {
if (dis == && depth == k) return ;
else return ;
}
if (vis[cur][dis][depth]) return dp[cur][dis][depth];
vis[cur][dis][depth] = true;
LL ans1 = dfs(cur + , dis, depth);
ans1 += dfs(cur + , dis + , max(dis + , depth));
if (dis > ) {
ans1 += dfs(cur + , dis - , depth);
}
return dp[cur][dis][depth] = ans1 % MOD;
}
void work() {
cin >> n >> k;
cout << dfs(, , ) << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
B. Dispersed parentheses 记忆化搜索 + 括号序列的状压表示的更多相关文章
- [bzoj2461][BeiJing2011][符环] (括号配对+记忆化搜索+高维dp)
Description 在可以炼制魔力强大的法杖的同时,Magic Land 上的人们渐渐意识到,魔力强大并不一定能给人们带来好处——反而,由此产生的破坏性的高魔力释放,给整个大陆蒙上了恐怖的阴影. ...
- trie上记忆化搜索,括号匹配——cf1152D好题!
一开始以为是卡特兰数的性质,,后来发现其实是dp,但是用记忆化搜索感觉更方便一点先来考虑字典树上的问题 设要求的序列长度是2n,我们用二元组(a,b)来表示前面长为a的序列中出现的 '(' - ')' ...
- Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索
https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...
- 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...
- HDU 4597 Play Game (DP,记忆化搜索,博弈)
题意:Alice和Bob玩一个游戏,有两个长度为N的正整数数字序列,每次他们两个,只能从其中一个序列,选择两端中的一个拿走.他们都希望可以拿到尽量大的数字之和, 并且他们都足够聪明,每次都选择最优策略 ...
- CodeForces 132C Logo Turtle (记忆化搜索)
Description A lot of people associate Logo programming language with turtle graphics. In this case t ...
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...
- 专题1:记忆化搜索/DAG问题/基础动态规划
A OpenJ_Bailian 1088 滑雪 B OpenJ_Bailian 1579 Function Run Fun C HDU 1078 FatMouse and Chee ...
- CF1028G Guess the Numbers 构造、记忆化搜索
传送门 考虑如果我们当前可以询问\(x\)个数,还剩下\(q\)次询问机会,我们要怎么构造询问方式? 肯定会这么考虑: 找到一个尽可能大的\(P\)满足\([x,P]\)能在每一次能询问\(x\)个数 ...
随机推荐
- 基于BASYS2的VHDL程序——数字钟
在编电子表时发现FPGA求余,取模只能针对2的次方.毕竟是数字的嘛! 时钟用到了动态刷新数码管.以一个大于50Hz的速度刷新每一个数码管. 因为数码管只有四个,只写了分针和秒针. 代码如下: libr ...
- Spring MVC 注解开发详解
@Controller控制器定义 1.Controller是单利模式,被多个线程请求共享,因此设计成无序状态. 2.通过@controller标注即可将class定义为一个controller类.为使 ...
- 详细的解说public,protected,Default和private的权限问题
详细的解说public,protected,Default和private的权限问题 让人更好的了解public,protected,Default和private他们之间的权限问题,我会做一个直观的 ...
- Linux-内存进程和软件安装
1 swap分区 swapon -s 查看swap分区 mkswap 某分区挂载点 swapon -a 分区 激活该swap swapoff 挂载点 取消swap分区 2 内存 free 查看内存空间 ...
- CodeForces768B:Code For 1 (分治)
Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On hi ...
- Python ip与数字的转换方式
例子:IP:192.168.1.10 方法一: In [1]: bin(192)Out[1]: '0b11000000' In [2]: bin(168)Out[2]: '0b10101000' In ...
- c++之cin/cin.get/cin.getline()详解
C++输入过程中,是把输入加载到缓冲区中,然后对缓冲区中的字符进行读取.cin,cin,get(),cin.getline()三个函数虽然都能进行数据读取,但是它们对缓冲区内数据的处理方法是不同的(如 ...
- PHP错题误区
1,$bool = TRUE;echo gettype($bool); //这个输出类型:booleanecho is_string($bool); //这个用echo是不能输出布尔型的,只有va ...
- dubbo项目中包的依赖说明
依赖 (+) (#) 必需依赖 JDK1.5+ 理论上Dubbo可以只依赖JDK,不依赖于任何三方库运行,只需配置使用JDK相关实现策略. 缺省依赖 通过mvn dependency:tree > ...
- URAL 1996 Cipher Message 3
题目 神题. 记得当初DYF和HZA讲过一个FFT+KMP的题目,一直觉得很神,从来没去做. 没有真正理解FFT的卷积. 首先考虑暴力. 只考虑前7位 KMP 找出所有 B 串可以匹配 A 串的位置. ...