DP的平行四邊形優化
阿新 • • 發佈:2018-12-19
Luogu P4767 [IOI2000]郵局
#include <bits/stdc++.h> using namespace std; int n,m,a[3005],mk[3005][3005]; long long dp[305][3005],w[3005][3005]; int main () { scanf("%d%d",&n,&m); for (int i = 1;i <= n;i++) scanf("%d",&a[i]); sort(a + 1,a + n + 1); for (int i = 1;i <= n;i++) for (int j = i + 1;j <= n;j++) w[i][j] = w[i][j - 1] + a[j] - a[(i + j) / 2]; for (int i = 1;i <= n;i++) dp[1][i] = w[1][i],mk[1][i] = 0; for (int i = 2;i <= m;i++) { mk[i][n + 1] = n; for (int j = n;j > i;j--) { dp[i][j] = 1ll << 60; for (int k = mk[i - 1][j];k <= mk[i][j + 1];k++) if (dp[i][j] > dp[i - 1][k] + w[k + 1][j]) dp[i][j] = dp[i - 1][k] + w[k + 1][j],mk[i][j] = k; } } printf("%lld",dp[m][n]); return 0; }