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\)个数 ...
随机推荐
- UITabBar 设置选中、未选中状态下title的字体颜色
一.如果只是设置选中状态的字体颜色,使用 tintColor 就可以达到效果 self.tabBar.tintColor = [UIColor redColor]; 二.但如果要将未选中状态和选中状 ...
- html5--3.13 表单的新增属性
html5--3.13 表单的新增属性 学习要点 掌握表单新增属性的使用 HTML5新增表单属性 之前课程中已经接触过的新增属性:autocomplete属性/autofocus属性/list属性/m ...
- 003 - 修改Pycharm的项目文件树样式
相信习惯了Eclipse或者Windows的小伙伴对于Pycharm的目录树一定觉得特别别扭 因为它总是在文件前加一个三角形标注, 这样的标注在视觉上十分误导层级关系 修改的方式为 File -& ...
- BZOJ_1004_[HNOI2008]Cards_burnside+DP
BZOJ_1004_[HNOI2008]Cards_burnside+DP Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问 ...
- 「LOJ#103」子串查找 (Hash
题目描述 这是一道模板题. 给定一个字符串 A A A 和一个字符串 B B B,求 B B B 在 A A A 中的出现次数.AAA 和 BBB 中的字符均为英语大写字母或小写字母. A A A 中 ...
- VC解析XML--使用CMarkup类解析XML
经过今天尝试MFC解析XML串,也算有了不少收获,总结一下. 我是使用的CMarkup类对XML进行操作. CMarkup好象都是先从一个xml文件里 ...
- Windows Vista for Developers——第四部分:用户帐号控制(User Account Control,UAC)
作者:Kenny Kerr 翻译:Dflying Chen 原文:http://weblogs.asp.net/kennykerr/archive/2006/09/29/Windows-Vista-f ...
- visualstudio2017 +EF+Mysql生成实体数据模型闪退
VisualStudio2017+EF+MySql正常运转,费了不少劲,踏过不少坑 1.安装 Connector/NET 8.0.13 地址:https://dev.mysql.com/downloa ...
- android项目源码
[置顶] Android精品开源项目整理_V20140221(持续更新中..) 让我们回顾下2013年有哪些精品资源:Android精品开源项目整理_V20131115(持续更新中..) 引言: ...
- 安装mosquitto报缺少dll文件的错误
解决:下载缺少的dll组件,放到安装目录. 报错:The procedure entry point CRYPTO_memcmp could not be located in the dynami ...