其他分享
首页 > 其他分享> > HDU 1421

HDU 1421

作者:互联网

HDU 1421

DP思路
#include <bits/stdc++.h>

using namespace std;
const int maxn = 2050;

int a[maxn];
int dp[maxn][maxn];
int n,m;

int power(int a,int b) {
    return (a - b)*(a - b);
}
int main() {
    while (scanf("%d%d",&n,&m) != EOF && (n+m)) {
        for (int i=1; i<=n; ++i)
            scanf("%d",&a[i]);
        sort(a+1,a+1+n);
        memset(dp,0x3f,sizeof dp);
        // 无论多少个i 一对没取 必须消耗值必须为0
        for (int i=0; i<=n; ++i) dp[i][0] = 0;
        // 从第2个物品开始dp
        for (int i=2; i<=n; ++i)
            for (int j=1; j*2<=i; ++j)
                dp[i][j] = min(dp[i-1][j],dp[i-2][j-1]+power(a[i],a[i-1]));
        printf("%d\n",dp[n][m]);
    }
    return 0;
}

标签:1421,HDU,min,int,num,maxn,物品,dp
来源: https://blog.csdn.net/qq_43580151/article/details/102766056