题目链接

BZOJ4824

题解

观察出题目中的关系实际上是完全二叉树的父子关系

我们设\(f[i][j]\)为以\(i\)为根的节点在其子树中排名为\(j\)的方案数

转移时,枚举左右子树分别有几个节点比\(i\)小,进行转移

乍一看是\(O(n^3)\)的,但其复杂度分析和某一题很像

就是在根处枚举两个子树大小,实质上就等于枚举任意两点\(lca\),是\(O(n^2)\)的

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
#define ls (u << 1)
#define rs (u << 1 | 1)
using namespace std;
const int maxn = 205,maxm = 100005,INF = 1000000000,P = 1000000007;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
LL C[maxn][maxn],f[maxn][maxn],siz[maxn],n,typ[maxn];
LL suml[maxn][maxn],sumr[maxn][maxn];
char s[maxn];
void init(){
for (int i = 0; i <= 100; i++){
C[i][0] = C[i][i] = 1;
for (int j = 1; j <= (i >> 1); j++)
C[i][j] = C[i][i - j] = (C[i - 1][j - 1] + C[i - 1][j]) % P;
}
}
void dfs(int u){
if (u > n) return;
dfs(ls); dfs(rs);
siz[u] = siz[ls] + 1 + siz[rs];
if (ls > n) f[u][1] = 1;
else if (rs > n){
for (int i = 0; i <= siz[ls]; i++)
if (!typ[ls]) f[u][i + 1] = sumr[ls][i + 1];
else f[u][i + 1] = suml[ls][i];
}
else {
LL t1,t2;
for (int i = 0; i <= siz[ls]; i++){
if (!typ[ls]) t1 = sumr[ls][i + 1];
else t1 = suml[ls][i];
for (int j = 0; j <= siz[rs]; j++){
if (!typ[rs]) t2 = sumr[rs][j + 1];
else t2 = suml[rs][j];
f[u][i + j + 1] = (f[u][i + j + 1] + C[i + j][i] * C[siz[u] - (i + j + 1)][siz[ls] - i] % P * t1 % P * t2 % P) % P;
}
}
}
for (int i = 1; i <= n ; i++) suml[u][i] = (suml[u][i - 1] + f[u][i]) % P;
for (int i = n; i >= 0; i--) sumr[u][i] = (sumr[u][i + 1] + f[u][i]) % P;
}
int main(){
init();
n = read();
scanf("%s",s + 2);
for (int i = 2; i <= n; i++)
typ[i] = s[i] == '<' ? 0 : 1;
dfs(1);
printf("%lld\n",suml[1][n]);
return 0;
}

最新文章

  1. TYVJ P1026 犁田机器人 Label:水
  2. 关于VECTOR和DEQUE
  3. struts2+hibernate-jpa+Spring+maven 整合(2)
  4. virtualbox共享文件夹无访问权限问题解决方法
  5. 设计模式&amp;UML学习
  6. Linux/UNIX进程控制(1)
  7. Redis安装介绍
  8. 201521123073《Java程序设计》第4周学习总结
  9. Vue.js-05:第五章 - 计算属性与监听器
  10. 杭电ACM2013--蟠桃记
  11. 敏捷开发——User Story
  12. 中软酒店管理系统CSHIS操作手册_数据结构_数据字典
  13. 爆炸销毁动画组件Explosions
  14. 为Firefox浏览器安装Firebug插件
  15. VirtualBox 克隆后 IP 地址相同(DHCP 分配),如何变更MAC以获取不同的IP?
  16. Matplotlib新手上路(下)
  17. Laravel JsonResponse数组获取
  18. MySQL Proxy和 Amoeba 工作机制浅析
  19. 读书笔记 ~ Python黑帽子 黑客与渗透测试编程之道
  20. curl: (6) Couldn’t resolve host ‘www.ttlsa.com’【转】

热门文章

  1. angular学习之angular-phonecat项目的实现
  2. 谈谈对bug的一点想法,说说做好日志记录的重要性
  3. pytorch中torch.nn构建神经网络的不同层的含义
  4. wps取消英文首字母大写功能
  5. c语言可变参数函数
  6. mysql sum 为 0 的解决方法
  7. 排序-InsertSort
  8. SpringMVC---简单登录例子
  9. 1 Django初探
  10. P1330 封锁阳光大学(染色问题)