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\)个数 ...
随机推荐
- 未公开函数MessageBoxTimeOut 实现定时消息(ZT) MFC实现MessageBox自动消失
http://www.blogjava.net/baicker/archive/2007/07/13/130072.html #include <windows.h> #include & ...
- android vector pathData探究,几分钟绘制自己的vectordrawable
之前经常看到一些酷酷的图标效果, 深入进去发现不是直接用的图片, 而是一些以Vector标签开头的xml文件, 于是就看到了如下代码: <vector xmlns:android="h ...
- 并不对劲的bzoj3677:p3647:[APIO2014]连珠线
题目大意 有一种生成\(n\)个点的树的方法为: 一开始有一个点,\(n-1\)次操作,每次可以有两种操作:1.选一个点,用一条红边将它与新点连接:2.将新点放在一条红边上,新点与这条红边两端点直接的 ...
- POJ1226(strstr)
Substrings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13348 Accepted: 4722 Descr ...
- spellchecker inspection helps locate typeos and misspelling in your code, comments and literals, and fix them in one click
项目layout文件中出现 spellchecker inspection helps locate typos and misspelling in your code, comments and ...
- zabbix server、agent安装及使用
先准备yum源,当然你有打好的rpm包那更好 [root@linux-node1 ~]# cat /etc/yum.repos.d/zabbix.repo [zabbix] name=Zabbix O ...
- git add . 的时候遇到warning: LF will be replaced by CRLF inXXX 解决办法
$ git add . warning: LF will be replaced by CRLF in shop/Runtime/Cache/86bbc820c9ec1 d314a9c71cf5651 ...
- 4月超棒的JavaScript游戏开发框架推荐(1) – 51CTO.COM
基于JavaScript开发的游戏是唯一一个能够跨桌面,Web和移动三种平台的.… 查阅全文 ›
- Ubuntu 下编译Android 源代码
1.配置JDK 1.6 或者1.7(看情况配置,有的Android版本不能在1.7下运行) 2.配置环境:终端:(CTRL+ALT+T) $ sudo apt-get install git gnup ...
- 构建Maven项目
Maven(一)如何用Eclipse创建一个Maven项目 Maven学习总结(三)——使用Maven构建项目 Eclipse创建一个Maven Web项目 [项目管理和构建]十分钟教程,eclips ...