题目概述
给出
n
个数和
解题报告
+K
……想到剩余类,这样的话
n
个数就只有
根据贪心,我们将
示例程序
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=300000,maxk=5000;
int n,K,a[maxn+5],A,B;LL f[maxk+5][maxk+5];
int main()
{
freopen("program.in","r",stdin);
freopen("program.out","w",stdout);
scanf("%d%d",&n,&K);for (int i=1;i<=n;i++) scanf("%d",&a[i]);
sort(a+1,a+1+n);int L=(n-1)/K,R=L+1;
for (int i=1;i<=K;i++) if ((n-i)/K+1==L) A++; else B++;
memset(f,63,sizeof(f));f[0][0]=0;
for (int i=0;i<=A;i++)
for (int j=0;j<=B;j++)
{
int now=i*L+j*R+1;
if (i<A) f[i+1][j]=min(f[i+1][j],f[i][j]+a[now+L-1]-a[now]);
if (j<B) f[i][j+1]=min(f[i][j+1],f[i][j]+a[now+R-1]-a[now]);
}
return printf("%lld\n",f[A][B]),0;
}