# [IOI2000]邮局

## 输入输出样例

10 5
1 2 3 6 7 9 11 22 44 50

9

## 说明/提示

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 using namespace std;
6 typedef long long ll;
7 ll inf,a[3010];
8 int n,m,p[3010][3010];
9 ll sum[3010],f[3010][310];
10 ll get(int x,int y)
11 {
12     if (x>=y) return 0;
13     int mid=(y+x)/2;
14     return sum[y]-sum[mid]-1ll*(y-mid)*a[mid]+1ll*(mid-x)*a[mid]-sum[mid-1]+sum[x-1];
15 }
16 int main()
17 {int i,j,k;
18     scanf("%d%d",&n,&m);
19     for (i=1;i<=n;i++)
20     {
21         scanf("%lld",&a[i]);
22     }
23     sort(a+1,a+n+1);
24     for (i=1;i<=n;i++)
25     sum[i]=sum[i-1]+a[i];
26     memset(f,0x3f,sizeof(f));
27     inf=f[0][0];
28     for (i=1;i<=m;i++)
29     f[i][i]=0,p[n+1][i]=n;
30     for (i=1;i<=n;i++)
31     f[i][1]=get(1,i),p[i][1]=0;
32     for (i=2;i<=m;i++)
33     {
34         for (j=n;j>i;j--)
35         {
36             for (k=p[j][i-1];k<=p[j+1][i];k++)
37             {
38                 if (k+1>j) continue;
39                 ll t=f[k][i-1]+get(k+1,j);
40                 if (f[j][i]>t)
41                 {
42                     f[j][i]=t;
43                     p[j][i]=k;
44                 }
45             }
46         }
47     }
48     printf("%lld\n",f[n][m]);
49 } 

posted @ 2020-01-30 20:08  Z-Y-Y-S  阅读(193)  评论(1编辑  收藏