题面
题解
其实就是一颗二叉树
我们假设左儿子小于根,右儿子大于根
考虑树形(dp)
(f[u][i])表示以(u)为根的子树,(u)为第(i)小
那么考虑子树合并
其实就是两个序列的合并
如果是左子树
枚举(j)为子树内有多少个数小于其根
那么以(i)为根的子树一定至少有(j)个
那么我们换一下枚举的东西
改成枚举有多少个数小于(u)
剩下的问题就是分配权值的问题了
组合数计算一下(具体看代码注释)
Code
#include<bits/stdc++.h>
using namespace std;
const int N = 110, Mod = 1e9 + 7;
int f[N][N], g[N][N], tmp[N], n, siz[N], C[N][N];
char s[N];
struct node {
int to, nxt;
}G[N];
int last[N], gl;
void add(int x, int y) {
G[++gl] = (node) {y, last[x]};
last[x] = gl;
}
void pls(int &x, int y) {
x += y;
if (x >= Mod) x -= Mod;
}
void dfs(int u) {
int ls = u << 1, rs = u << 1 | 1;
if (ls <= n) add(u, ls);
if (rs <= n) add(u, rs);
g[u][1] = f[u][1] = siz[u] = 1;
for (int z = last[u]; z; z = G[z].nxt) {
int v = G[z].to;
dfs(v);
for (int i = 1; i <= siz[u] + siz[v]; i++) tmp[i] = 0;
for (int i = 1; i <= siz[u]; i++)
for (int j = 0; j < siz[v]; j++)//子树v有j个数小于u(不包括v)
if (s[v] == '>')
pls(tmp[i + j + 1], 1ll * f[u][i] * g[v][j + 1] % Mod
* C[i + j][i - 1] % Mod * C[siz[u] + siz[v] - i - j - 1][siz[u] - i] % Mod);//一共有i+j数小于u,分配给i-1个位置权值 , siz[u] + siz[v] - i - j - 1个数大于u
else
pls(tmp[i + j], 1ll * f[u][i] * (g[v][siz[v]] - g[v][j] + Mod) % Mod
* C[i + j - 1][i - 1] % Mod * C[siz[u] + siz[v] - i - j][siz[u] - i] % Mod);
siz[u] += siz[v];
for (int i = 1; i <= siz[u]; i++) f[u][i] = tmp[i], g[u][i] = (g[u][i - 1] + f[u][i]) % Mod;
}
return ;
}
int main() {
scanf("%d%s", &n, s + 2);
for (int i = 0; i <= n; i++) C[i][i] = C[i][0] = 1;
for (int i = 2; i <= n; i++)
for (int j = 0; j < i; j++)
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % Mod;
dfs(1);
printf("%d
", g[1][n]);
return 0;
}