正题
luogu 2803
题目大意
给出n个点,和相邻的点的距离,每个点有一个权值,现在让你建k个特殊点,使所有点到其中一个特殊点的代价之和最小
解题思路
先预处理处一个区间到同一个特殊点的最小代价
然后DP即可
–
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define N 111
using namespace std;
int n, m, s[N], v[N], l[N], r[N], a[N][N], f[N][15];
int jsl(int x, int k)
{return r[x] - r[k] - (s[k - 1] - s[x - 1]) * (v[n] - v[k]);
}
int jsr(int y, int k)
{return l[y] - l[k] - (s[y] - s[k]) * v[k];
}
int main()
{scanf("%d%d", &n, &m);if (m >= n){puts("0");return 0;}for (int i = 1; i <= n; ++i){scanf("%d", &s[i]);s[i] += s[i - 1];}for (int i = 2; i <= n; ++i){scanf("%d", &v[i]);v[i] += v[i - 1];}for (int i = 1; i <= n; ++i)//处理前缀后缀{l[i] = (s[i] - s[i - 1]) * v[i];l[i] += l[i - 1];}for (int i = n; i > 0; --i){r[i] = (s[i] - s[i - 1]) * (v[n] - v[i]);r[i] += r[i + 1];}memset(a, 127/3, sizeof(a));memset(f, 127/3, sizeof(f));f[0][0] = 0;for (int i = 1; i <= n; ++i)for (int j = i; j <= n; ++j)for (int k = i; k <= j; ++k)a[i][j] = min(a[i][j], jsl(i, k) + jsr(j, k));//计算一个区间到同一个特殊点的最小代价for (int i = 1; i <= n; ++i)for (int j = 1; j <= i; ++j)for (int k = 1; k <= m; ++k)f[i][k] = min(f[i][k], f[j - 1][k - 1] + a[j][i]);//DPprintf("%d", f[n][m]);return 0;
}