说明

  1. Catalan(i) 表示卡特兰数的第 i 项。

题目链接:http://codeforces.com/problemset/problem/1152/C

题目大意

  有 n 个左括号和 n 个右括号,它们一共可以组成 Catalan(n) 个合法括号字符串,把这些字符串组建成 Trie 树,求这棵树的二分图最大匹配。

分析

  设 Node(L, R) 表示 Trie 树的一个节点,这个节点含有 L 个左括号和 R 个右括号。
  虽然说是求二分图最大匹配,不过这道题却不能用求最大匹配的的方法求(超时 + 爆栈),注意到这棵 Trie 是棵二叉树且根节点到每个叶子节点的距离都是一样的,都为 2*n,考虑第 2*i - 1 层和 2*i 层的边,首先 2*i 层的可选边数肯定大于等于 2*i - 1 层的可选边数,这是肯定的,因为越往下分支越多。而最优解在第 2*i - 1 层和 2*i 层的匹配情况在无非这三种之一:
  1. 全选第 2*i - 1 层的边。
  2. 全选第 2*i 层的边。
  3. 混合选。

  设策略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的更多相关文章

  1. codeforces#1152D. Neko and Aki's Prank(dp)

    题目链接: https://codeforces.com/contest/1152/problem/D 题意: 给出一个$n$,然后在匹配树上染色边,每个结点的所有相邻边只能被染色一次. 问,这颗树上 ...

  2. Neko and Aki's Prank CodeForces - 1152D (括号序列,dp)

    大意: 将所有长度为2*n的合法括号序列建成一颗trie树, 求trie树上选出一个最大不相交的边集, 输出边集大小. 最大边集数一定不超过奇数层结点数. 这个上界可以通过从底层贪心达到, 所以就转化 ...

  3. Codeforce Round #554 Div.2 D - Neko and Aki's Prank

    dp 找规律 我好菜啊好菜啊,完全没有思路. 在合法的括号序列中,左括号数一定大于等于右括号数的,所以我们可以先定义平衡度为左括号数-右括号数. 然后可以发现一个惊人的规律..就是在trie同一深度上 ...

  4. codeforces#1152C. Neko does Maths(最小公倍数)

    题目链接: http://codeforces.com/contest/1152/problem/C 题意: 给出两个数$a$和$b$ 找一个$k(k\geq 0)$得到最小的$LCM(a+k,b+k ...

  5. Codeforces C.Neko does Maths

    题目描述: C. Neko does Maths time limit per test 1 second memory limit per test 256 megabytes input stan ...

  6. CodeForces 1152E Neko and Flashback

    题目链接:http://codeforces.com/problemset/problem/1152/E 题目大意 有一个 1~n-1 的排列p 和长度为 n 的数组 a,数组b,c定义如下: b:b ...

  7. CodeForces 1152F2 Neko Rules the Catniverse (Large Version)

    题目链接:http://codeforces.com/problemset/problem/1152/F2 题目大意 见http://codeforces.com/problemset/problem ...

  8. CodeForces 1152F1 Neko Rules the Catniverse (Small Version)

    题目链接:http://codeforces.com/problemset/problem/1152/F1 题目大意 有 n 个星球,给定限制 m,从 x 星球走到 y 星球的条件是,$1 \leq ...

  9. Codeforces 1152D(dp)

    要点 寻找最多边的匹配的结论:贪心地从叶子开始找,最后答案都是奇数层下边的那条边. 设\(dp[i][j]\)表示当前长度为\(i\),平衡度为\(j\),平衡度为(数量减去)数量. 增加左右括号转移 ...

随机推荐

  1. Shiro学习(15)单点登录

    Shiro 1.2开始提供了Jasig CAS单点登录的支持,单点登录主要用于多系统集成,即在多个系统中,用户只需要到一个中央服务器登录一次即可访问这些系统中的任何一个,无须多次登录.此处我们使用Ja ...

  2. Tomcat启动报:The Server time zone value 'XXXXX' 乱码问题解决

    今天给自己项目打包到服务器发布时,运行时,发现报 java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecogniz ...

  3. BZOJ 2326: [HNOI2011]数学作业(矩阵乘法)

    传送门 解题思路 NOIp前看到的一道题,当时想了很久没想出来,NOIp后拿出来看竟然想出来了.注意到有递推\(f[i]=f[i-1]*poww[i]+i\),\(f[i]\)表示\(1-i\)连接起 ...

  4. SDNU 1217 CD收藏——并查集

    Description     lmh平常爱听歌,所以买了很多的CD来收藏,但是因为平常整理不当,所以忘记了这些CD的歌手是谁.现在他想知道他到底收藏了多少位歌手的专辑,于是他想了一个办法,同时拿出两 ...

  5. 在Linux下安装PyEmu

    git clone https://github.com/OpenRCE/pydbg.git git clone https://github.com/OpenRCE/paimei.git libda ...

  6. POJ3356-AGTC-dp+字符串处理

    Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing ...

  7. Git 学习第四天

    我们已经知道,通过命令 git remote add origin git@github.com/Your.name/file.git 可以连接远程仓库,那么,假如我现在想切换另个一远程仓库的连接应该 ...

  8. Red Hat Enterprise Linux 7.7 使用最小化安装后,怎么安装桌面的解决方法

    准备工具: xshell6,xftp6,到官网(https://www.netsarang.com/zh/downloading/)进行下载,教育版的,个人使用 虚拟机安装教程百度即可,安装时有两个重 ...

  9. JUC源码分析-集合篇(四)CopyOnWriteArrayList

    JUC源码分析-集合篇(四)CopyOnWriteArrayList Copy-On-Write 简称 COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个人想 ...

  10. 【非官方方式】获取Disconf动态更新的配置文件的值

    disconf可以配置reload,当更改配置时自动刷新classpath下的配置文件.然而获取最新的值官方说明是加@DisconfFileItem注解放在属性的方法上,底层通过拦截器获取的. 但是每 ...