DP的平行四边形优化

Luogu P4767 [IOI2000]邮局

#include 
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;
}

你可能感兴趣的:(DP,DP优化,模板)