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\)个数 ...
随机推荐
- 【mysql】mysql innodb 配置详解
MySQL innodb 配置详解 innodb_buffer_pool_size:这是InnoDB最重要的设置,对InnoDB性能有决定性的影响.默认的设置只有8M,所以默认的数据库设置下面Inno ...
- ansible使用中遇到的问题
前提是,可以ssh无秘钥过去,但是使用ansible就报这个错误, 正在找造成的原因及解决方法 第一步, 明白了,,如何已经打通ssh无秘钥后,就不能再 hosts中加入ansible_ssh_pas ...
- jQuery comet
下面程序是例用从数据端推送信息,原理是每隔10秒读取一下data.txt文件,看有木有新的数据输入,如果有,则alert文件内容. hmtl代码是 <!DOCTYPE html> < ...
- [转载]Dalvik指令集
这篇文章是转载的,为了便于查找一些指令,贴在这里. 转自:http://blog.csdn.net/canfengxiliu/article/details/20144119 ------------ ...
- AutoIt中ControlFocus的使用
在使用AutoIt最控件做自动化操作的时候,经常性的会碰到无法使用Windows Info工具获取控件的属性,但是我们又需要获取该控件的焦点,我们该怎么办呢? 方法1: 应用controlFocus方 ...
- poj 2069 Super Star —— 模拟退火
题目:http://poj.org/problem?id=2069 仍是随机地模拟退火,然而却WA了: 看看网上的题解,都是另一种做法——向距离最远的点靠近: 于是也改成那样,竟然真的A了...感觉这 ...
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bireportSqlSessionFactory' defined in URL
报错如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'birepo ...
- json对象和json数组
json字符串对象和json字符串数组:JSONArray跟JSONObject的区别就是JSONArray比JSONObject多中括号[] jsonObject: "Row": ...
- D - Opponents
Description Arya has n opponents in the school. Each day he will fight with all opponents who are pr ...
- linux磁盘存储管理基本命令和工具
1 磁盘在linux表示方法 (1) IDE硬盘:hd[a~z]x,主设备号+次设备号+磁盘分区编号/hd(0-n,y) (2)SCSI硬盘:sd[a~z]x/hd(0-n,y) 注:主设备号可以唯一 ...