比赛 2022级DP专题练习赛1 评测结果 WWAWAWWWWA
题目名称 二叉查找树 最终得分 30
用户昵称 zxhhh 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2023-02-10 20:43:53
显示代码纯文本
#include <bits/stdc++.h>

using namespace std;

const int N = 75;
typedef long long ll;

int n;
ll dp[N][N], k, m[N][N], s[N][N];

struct node {
	ll a, v, s;
}nodes[N];

bool cmp (node x, node y) { 
	return x.a < y.a;
} 

int main () {
	freopen("treapmod.in", "r", stdin);
	freopen("treapmod.out", "w", stdout);
	memset(m, 0x3f, sizeof(m)); memset(dp, 0x3f, sizeof(dp));
	scanf("%d%lld", &n, &k);
	for (int i = 1;i <= n;i++) scanf("%lld", &nodes[i].a);
	for (int i = 1;i <= n;i++) scanf("%lld", &nodes[i].v);
	for (int i = 1;i <= n;i++) scanf("%lld", &nodes[i].s);
	sort(nodes + 1, nodes + 1 + n, cmp);
	for (int i = 1;i <= n;i++) dp[i][i] = nodes[i].s;
	for (int l = 1;l <= n;l++) {
		for (int i = 1;i + l - 1 <= n;i++) {
			int j = i + l - 1;
			for (int z = i;z <= j;z++) s[i][j] = s[i][j] + nodes[z].s, m[i][j] = min(m[i][j], nodes[z].v);
		}
	}
	for (int l = 2;l <= n;l++) {
		for (int i = 1;i + l - 1 <= n;i++) {
			int j = i + l - 1;
//			m[i][j] = min(m[i][i], m[i + 1][j]); s[i][j] = s[i][i] + s[i + 1][j]; 
			for (int z = i;z <= j;z++) {
				ll ls = 0, rs = 0, ad = 0;
				if (nodes[z].v > m[i][j]) ad = k;
				if (z > i) ls = dp[i][z - 1];
				if (z < j) rs = dp[z + 1][j];
				dp[i][j] = min(dp[i][j], ls + rs + ad + s[i][j]);
			} 
		}
	}
	printf("%lld\n", dp[1][n]);
	return 0;
}