0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 18:48:48 +00:00

P1040 [NOIP2003 提高组] 加分二叉树

R73670543
This commit is contained in:
Baoshuo Ren 2022-04-10 16:51:59 +08:00
parent 4e67b54a86
commit 9b3f4d7978
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

52
Luogu/P1040/P1040.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <iostream>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 35;
int n, f[N][N], root[N][N];
void dfs(int l, int r) {
if (l > r) return;
cout << root[l][r] << ' ';
if (l == r) return;
dfs(l, root[l][r] - 1);
dfs(root[l][r] + 1, r);
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> f[i][i];
f[i][i - 1] = 1;
root[i][i] = i;
}
for (int len = 2; len <= n; len++) {
for (int l = 1, r = len; r <= n; l++, r++) {
f[l][r] = f[l][l] + f[l + 1][r];
root[l][r] = l;
for (int k = l + 1; k < r; k++) {
if (f[l][r] < f[l][k - 1] * f[k + 1][r] + f[k][k]) {
f[l][r] = f[l][k - 1] * f[k + 1][r] + f[k][k];
root[l][r] = k;
}
}
}
}
cout << f[1][n] << endl;
dfs(1, n);
cout << endl;
return 0;
}