The central sequence traversal of a tree is unique. After the mid-order traversal is processed according to the data value, the DP (L, R, v) represents a tree consisting of [L, R], and the minimum cost of the weight of all nodes of the tree ≥v (discretization weights).
The enumeration m is the root (p indicates the frequency of access):
Modify the weight of M: DP (L, r, v) = min (DP (l, m-1, V) + DP (m+1, R, v) + p (l~r) + K)
Do not modify (m original weight ≥v): DP (L, r, v) = min (DP (l, M-1, Value (m)) + DP (m+1, R, Value (m)) + P (l~r))
Time complexity o (n log n + n^4)
-------------------------------------------------------------------------------
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long Long ll;const int MAXN =;Const LL inf = 1LL <<;int N, C, H[MAXN];ll DP[MAXN][MAXN][MAXN]; Template<class t>inline void Min (t &x, T t) {if (t < x) x = t;}struct Node {int D, V, p;BOOL Operator < (const Node &o) Const {return D < O.D;}} O[MAXN];ll Dp (int l, int r, int v) {if (L > R) return 0;ll &t = dp[l][r][v];if (~t) return t;int d = O[R].P-O[L-1].P;if (L = = r)return T = d + (o[l].v >= v? 0:c);t = inf;for (int i = l; I <= R; i++) {Min (T, DP (L, i-1, V) + DP (i + 1, R, v) + C + D);if (o[i].v >= v)Min (T, DP (L, I-1, O[I].V) + DP (i + 1, R, O[I].V) + D);}return t;}int main () {scanf ("%d%d", &n, &c);for (int i = 1; I <= N; i++) scanf ("%d", &o[i].d);for (int i = 1; I <= N; i++) scanf ("%d", &o[i].v);for (int i = 1; I <= N; i++) scanf ("%d", &O[I].P);sort (o + 1, O + N + 1);o[0].p = 0;for (int i = 1; I <= N; i++) {O[I].P + = O[I-1].P;h[i-1] = o[i].v;}sort (H, H + N);int hn = unique (H, H + N)-H;for (int i = 1; I <= N; i++)o[i].v = Lower_bound (H, H + hn, O[I].V)-H;memset (DP,-1, sizeof DP);ll ans = inf;for (int i = 0; i < HN; i++)Min (ans, Dp (1, N, i));cout << ans << "\ n";return 0;}
-------------------------------------------------------------------------------
1564: [NOI2009] binary search tree time limit: ten Sec Memory Limit: MB
Submit: 625 Solved: 453
[Submit] [Status] [Discuss] Descriptioninputoutput has only one number, which is the minimum value that you can get from the cost of accessing the entire tree and the cost of the additional modification. Sample Input4 10
1 2 3 4
1 2 3 4
1 2 3 4
Sample Output29
HINT
The original image of the input is left, and its access cost is 1x1+2x2+3x3+4x4=30. The best modification scheme is to change the weight of the 3rd node in the input to 0, get the right image, the access cost is 1x2+2x3+3x1+4x2=19, plus the additional modification cost 10, altogether 29.
Source
Bzoj 1564: [NOI2009] Binary lookup tree (DP)