本文主要是介绍hdu 1421,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <map> #include <algorithm> #include <set> using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long ll; typedef unsigned long long ULL; const int mod = 1000000007; const double eps = 1e-10; const int inf = 0x3f3f3f3f; const int big=50000; int max(int a,int b) {return a>b?a:b;}; int min(int a,int b) {return a<b?a:b;}; int a[2005],dp[2005][2005]; int main() {int n,k;while(~scanf("%d %d",&n,&k)){for(int i=1;i<=n;i++)scanf("%d",&a[i]);sort(a+1,a+n+1);for(int i=0;i<=k;i++){dp[0][i]=0;dp[1][i]=inf;}for(int i=0;i<=n;i++)dp[i][0]=0;for(int i=2;i<=n;i++)for(int j=1;j<=k;j++)if(2*j>i)dp[i][j]=inf;elsedp[i][j]=min(dp[i-1][j],dp[i-2][j-1]+(a[i]-a[i-1])*(a[i]-a[i-1]));cout<<dp[n][k]<<endl;}return 0; }
这篇关于hdu 1421的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!