D. Minimization
time limit per test 

2 seconds

memory limit per test 

256 megabytes

input 

standard input 

output 

standard output

You've got array A, consisting of n integers and a positive integer k. Array A is indexed by integers from 1 to n.

You need to permute the array elements so that value

became minimal possible. In particular, it is allowed not to change order of elements at all.

Input

The first line contains two integers n, k (2 ≤ n ≤ 3·105, 1 ≤ k ≤ min(5000, n - 1)).

The second line contains n integers A[1], A[2], ..., A[n] ( - 109 ≤ A[i] ≤ 109), separate by spaces — elements of the array A.

Output

Print the minimum possible value of the sum described in the statement.

You've got array A, consisting of n integers and a positive integer k. Array A is indexed by integers from 1 to n.

You need to permute the array elements so that value

became minimal possible. In particular, it is allowed not to change order of elements at all.

Input

The first line contains two integers n, k (2 ≤ n ≤ 3·105, 1 ≤ k ≤ min(5000, n - 1)).

The second line contains n integers A[1], A[2], ..., A[n] ( - 109 ≤ A[i] ≤ 109), separate by spaces — elements of the array A.

Output

Print the minimum possible value of the sum described in the statement.

按照下标取模后的值可以分成k组,对于一组来说,按照升序排相邻的差之和是最小的,可用交换法证明,不难看出,总和等于a[end]-a[start]。对于不同的两组,

公共元素一定在端点,同样交换法可证,因此将整个数组排序以后相同一组一定连续。有n%k个长度为n/k+1的,其他的长度为n/k。

因此需要决策长度的选法,定义dp[i][j]表示选了i个长度为n/k+1,j个长度为n/k的组的最小花费。那么决策是下一个区间是长或者是短,边界条件dp[0][0] = 0表示什么也不选的时候花费为0。dp[i][j] = min(dp[i-1][j]+cost(i-1,j,L),dp[i][j-1]+cost(i,j-1,S))。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+, maxk = ;
int a[maxn];
int dp[][maxk];
int n,k,len; int cost(int i,int j,int L)
{
int s = (i+j)*len+i, e = s+len+L-;
return a[e]-a[s];
} int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&k);
for(int i = ; i < n; i++) scanf("%d",a+i);
sort(a,a+n);
int tot = k, r = n%k;
int L = r, S = tot-r;
len = n/k;
for(int i = ; i <= L; i++){
int cur = i&, pre = cur^;
for(int j = ; j <= S; j++){
if(i && j) dp[cur][j] = min(dp[pre][j]+cost(i-,j,),dp[cur][j-]+cost(i,j-,));
else if(i) dp[cur][j] = dp[pre][j]+cost(i-,j,);
else if(j) dp[cur][j] = dp[cur][j-]+cost(i,j-,);
else dp[cur][j] = ;
}
}
printf("%d",dp[L&][S]);
return ;
}

Codeforces Round #317 (Div. 2) D Minimization (贪心+dp)的更多相关文章

  1. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

  2. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  3. Codeforces Round #202 (Div. 1) A. Mafia 贪心

    A. Mafia Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...

  4. Codeforces Round #382 (Div. 2)B. Urbanization 贪心

    B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...

  5. Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp

    题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...

  6. Codeforces Round #180 (Div. 2) B. Sail 贪心

    B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...

  7. Codeforces Round #192 (Div. 1) A. Purification 贪心

    A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...

  8. Codeforces Round #274 (Div. 1) A. Exams 贪心

    A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...

  9. Codeforces Round #374 (Div. 2) B. Passwords 贪心

    B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...

随机推荐

  1. zz 堆空间与栈空间

    http://blog.sina.com.cn/s/blog_7321be1101013aua.htmlhttp://soft.chinabyte.com/os/51/12324551.shtmlht ...

  2. app自动化测试工具robotium

    robotium基于instramentation框架,可对app白盒黑盒测试,缺点是测试进程和被测进程需要在一个进程中,不能跨应用 白盒测试时,需要app源代码,在eclipse里新建android ...

  3. Qt-MVC图形视图框架分解

    前面在<Qt-MVC图形视图框架出识>中我们了解了Qt图形视图框架中三个最基本的类,弄清他们的关系,本片小文,我们将对QGraphicsView,QGraphiceScene,QGraph ...

  4. 洛谷 - P1414 - 又是毕业季II - 因数

    https://www.luogu.org/problemnew/show/P1414 以后这种gcd的还是尽可能往分解那里想一下. 先把每个数分解,他的所有因子都会cnt+1. 然后从最大的可能因子 ...

  5. 无法打开编译器生成的文件:“../../build/vs71/release/v100/MD_MBCS\json_value.

    1>正在生成代码 1>e:\Source\VC\?\json\jsoncpp-src-0.6.0-rc2\src\lib_json\json_value.cpp : fatal error ...

  6. ZOJ3321,ZOJ3317

    ZOJ3321 //there is at most one edge between two nodes. 因为这句话的局限性,又要满足环,那么一定是每个点度为2,然后为n节点的一个环 //#inc ...

  7. POJ3692【二分匹配】

    题意: 有男生女生,男生都认识双方,女生都认识双方,给出一些男女关系,问最大拿多少个人,使得所有人都认识双方. 思路: 原图最大团=总结点数-[[补图(补图为二分图)]的最大独立集=最大完全子图的顶点 ...

  8. HDU 3499【最短路】

    题意: 给你一幅图,然后起点终点,然后有一个条件是可以使某条边的花费减半,求最短路的最小花费. 思路: (来自大哥) 最短路的时候多一维,途中是否有花费减半的边: 然后转移,如果上一条有减半的,这一条 ...

  9. 传统JDBC操作数据库

    package com.jdbc.example; import java.sql.Connection; import java.sql.Date; import java.sql.DriverMa ...

  10. 从图(Graph)到图卷积(Graph Convolution):漫谈图神经网络模型 (一)

    本文属于图神经网络的系列文章,文章目录如下: 从图(Graph)到图卷积(Graph Convolution):漫谈图神经网络模型 (一) 从图(Graph)到图卷积(Graph Convolutio ...