hdu 1500 Chopsticks

时间:2023-03-10 00:47:00
hdu 1500 Chopsticks

http://acm.hdu.edu.cn/showproblem.php?pid=1500

dp[i][j]为第i个人第j个筷子。

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int dp[][];
int a[];
int k,n;
int sqr(int x)
{
return x*x;
}
bool cmp(const int a,const int b)
{
return a>b;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&k,&n);
memset(dp,,sizeof(dp));
memset(a,,sizeof(a));
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
sort(a+,a+n+,cmp);
k=k+;
for(int i=; i<=k; i++)
{
dp[i][i*]=dp[i-][i*-]+sqr(a[i*-]-a[i*]);
for(int j=i*+; j<=n; j++)
{
dp[i][j]=min(dp[i][j-],dp[i-][j-]+sqr(a[j-]-a[j]));
}
}
printf("%d\n",dp[k][n]);
}
return ;
}