CodeForces 1152D Neko and Aki's Prank
说明
- Catalan(i) 表示卡特兰数的第 i 项。
题目链接:http://codeforces.com/problemset/problem/1152/C
题目大意
有 n 个左括号和 n 个右括号,它们一共可以组成 Catalan(n) 个合法括号字符串,把这些字符串组建成 Trie 树,求这棵树的二分图最大匹配。
分析
- 全选第 2*i - 1 层的边。
- 全选第 2*i 层的边。
- 混合选。
设策略1在每一层所选的边数为:A1,A2,……A2n。(A2i == 0)
设策略2在每一层所选的边数为:B1,B2,……B2n。(B2i-1 == 0)
设策略3在每一层所选的边数为:C1,C2,……C2n。
首先,策略1肯定不是最优解,因为对于策略1的每一非0项 A2i-1 都有 B2i >= A2i-1。同理,策略3也不是,因为选择第 2*i - 1 层的一条边必然要取消选择第 2*i 层的对应边,策略3顶多和策略2一样优。
因此匹配只考虑策略2即可。
代码如下
#include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< string, int > PSI;
typedef set< int > SI;
typedef vector< int > VI;
typedef map< int, int > MII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e3 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; int n;
// dp[l][r]表示以当前节点(已经选了 l 个左括号和 r 个右括号)为根的子树的最大匹配种数。
LL dp[maxN][maxN]; int main(){
INIT();
cin >> n;
dp[n][n] = ;
// 枚举 l + r
rFor(k, * n - , ) {
int tmp = min(n, k);
// tmp 为 l 的上限,(k + 1) / 2 为 l 的下限
// 下限的选取保证了 l >= r
rFor(l, tmp, (k + ) / ) {
int r = k - l;
//assert(l >= r);
if(l < n) dp[l][r] += dp[l + ][r];
dp[l][r] += dp[l][r + ];
dp[l][r] += (l + r) % ; // 只要匹配偶数层即可
dp[l][r] %= mod;
}
} cout << dp[][] << endl;
return ;
}
CodeForces 1152D Neko and Aki's Prank的更多相关文章
- codeforces#1152D. Neko and Aki's Prank(dp)
题目链接: https://codeforces.com/contest/1152/problem/D 题意: 给出一个$n$,然后在匹配树上染色边,每个结点的所有相邻边只能被染色一次. 问,这颗树上 ...
- Neko and Aki's Prank CodeForces - 1152D (括号序列,dp)
大意: 将所有长度为2*n的合法括号序列建成一颗trie树, 求trie树上选出一个最大不相交的边集, 输出边集大小. 最大边集数一定不超过奇数层结点数. 这个上界可以通过从底层贪心达到, 所以就转化 ...
- Codeforce Round #554 Div.2 D - Neko and Aki's Prank
dp 找规律 我好菜啊好菜啊,完全没有思路. 在合法的括号序列中,左括号数一定大于等于右括号数的,所以我们可以先定义平衡度为左括号数-右括号数. 然后可以发现一个惊人的规律..就是在trie同一深度上 ...
- codeforces#1152C. Neko does Maths(最小公倍数)
题目链接: http://codeforces.com/contest/1152/problem/C 题意: 给出两个数$a$和$b$ 找一个$k(k\geq 0)$得到最小的$LCM(a+k,b+k ...
- Codeforces C.Neko does Maths
题目描述: C. Neko does Maths time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CodeForces 1152E Neko and Flashback
题目链接:http://codeforces.com/problemset/problem/1152/E 题目大意 有一个 1~n-1 的排列p 和长度为 n 的数组 a,数组b,c定义如下: b:b ...
- CodeForces 1152F2 Neko Rules the Catniverse (Large Version)
题目链接:http://codeforces.com/problemset/problem/1152/F2 题目大意 见http://codeforces.com/problemset/problem ...
- CodeForces 1152F1 Neko Rules the Catniverse (Small Version)
题目链接:http://codeforces.com/problemset/problem/1152/F1 题目大意 有 n 个星球,给定限制 m,从 x 星球走到 y 星球的条件是,$1 \leq ...
- Codeforces 1152D(dp)
要点 寻找最多边的匹配的结论:贪心地从叶子开始找,最后答案都是奇数层下边的那条边. 设\(dp[i][j]\)表示当前长度为\(i\),平衡度为\(j\),平衡度为(数量减去)数量. 增加左右括号转移 ...
随机推荐
- 51nod-1366 贫富差距——并查集
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1366 #include <iostream> # ...
- JBoss、Tomcat、JBoss EAP、JBoss AS、wildfly,JBoss EAP安装部署,JBoss各个版本下载,JBoss允许远程访问
感谢: https://www.cnblogs.com/invlong/p/5983334.html https://blog.csdn.net/mooncarp/article/details/78 ...
- ( 转)WPF面板布局介绍Grid、StackPanel、DockPanel、WrapPanel
回顾 上一篇,我们介绍了基本控件及控件的重要属性和用法,我们本篇详细介绍WPF中的几种布局容器及每种布局容器的使用场景,当 然这些都是本人在实际项目中的使用经验,可能还存在错误之处,还请大家指出. 本 ...
- C# 中如何输出双引号(转义字符的使用)
实现效果: 输出这样的一个含有双引号的字符串 "hello" 方式一: 不用 @ 时转义 System.Console.WriteLine("\"he ...
- Xen的半虚拟化(Paravirtualization)
三个特权级 IA-32体系提供了4个特权级别,正常情况下只用了2个, 操作系统运行在Ring 0,而应用程序运行在Ring 3. Xen让自己运行在Ring 0, 而操作系统运行在Ring 1, 应用 ...
- 牛客 判断t1树中是否含有与t2树拓扑结构完全相同的子树
题目链接:https://www.nowcoder.com/practice/5a41ce17e8194e1688aa83a73137f7ee?tpId=101&tqId=33239& ...
- 剑指offer——73股票的最大利润
题目: 假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可能获得的最大利润是多少?例如,一只股票在某些时间节点的价格为{9,11,8,5,7,12,16,14}.如果我们能在价格为5 ...
- java.lang.Boolean.compareTo()方法实例
compareTo接口 Comparable<Boolean>指定以下接口 参数 b - 布尔实例进行比较 返回值 方法返回 0 - 如果该对象表示相同的布尔值作为参数 一个正数值 - 如 ...
- Day 21 python :面向对象 类的相关内置函数 /单例模式 /描述符
1.isinstance(obj,cls) 检查obj是否是类cls的对象: 备注:用isinstance 的时候,产生实例后,会显示实例既是父类的实例,也是子类的实例 class Mom: gend ...
- Java 并发理论简述
一:为什么需要多线程? 线程是Java语言中不可或缺的重要部分,它们能使复杂的异步代码变得简单,简化复杂系统的开发:能充分发挥多处理器系统的强大计算能力.多线程和多进程的区别与选择可以参考我的另一篇博 ...