Codeforces Round #317 (Div. 2) D Minimization (贪心+dp)
2 seconds
256 megabytes
standard input
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.
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.
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.
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.
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)的更多相关文章
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- 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 ...
- Codeforces Round #382 (Div. 2)B. Urbanization 贪心
B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...
- Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp
题目链接: http://codeforces.com/problemset/problem/268/E E. Playlist time limit per test 1 secondmemory ...
- Codeforces Round #180 (Div. 2) B. Sail 贪心
B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...
- Codeforces Round #192 (Div. 1) A. Purification 贪心
A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...
- 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 ...
- Codeforces Round #374 (Div. 2) B. Passwords 贪心
B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...
随机推荐
- SqlServer2012——快照
1.数据库快照 优点: 维护历史数据以生成报表.由于数据库快照可提供数据库的静态视图,因而可以通过快照访问特定时间点的数据. 将查询实施在数据库的快照上,可以释放主体数据库上的资源. 数据库快照的限制 ...
- 1107 Social Clusters (30 分)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- Springboot整合elasticSearch的官方API实例
前言:在上一篇博客中,我介绍了从零开始安装ElasticSearch,es是可以理解为一个操作数据的中间件,可以把它作为数据的存储仓库来对待,它具备强大的吞吐能力和计算能力,其基于Lucene服务器开 ...
- SpringBoot2.0 基础案例(02):配置Log4j2,实现不同环境日志打印
一.Log4j2日志简介 日志打印是了解Web项目运行的最直接方式,所以在项目开发中是需要首先搭建好的环境. 1.Log4j2特点 1)核心特点 相比与其他的日志系统,log4j2丢数据这种情况少:d ...
- endless(2018.10.25)
这题就是个线段树合并板子. #include<cstdio> #include<algorithm> #include<cstring> using namespa ...
- 牛客练习赛42D(性质、数学)
题目传送 就像题解所说的,写几个可以发现有分成四段的性质:第一段是从n开始往下贪,第二段是个数字,第三段……卧槽好吧真难描述. 然后发现这个数据量可达1e9,所以考虑“二分确定序列+数学计算”的方式解 ...
- 转 OUI and OPatch Do Not Recognize JDK/JRE on Windows
issue 1: 新覆盖的opatch 提示,无法opatch 报错 此时不应有1.6 D:\app\Administrator\product\11.2.0\dbhome_1\OPatch\ocm\ ...
- JS=和==和===的区别
1. = : 赋值运算,赋值使用2.== :比较运算,仅比较自动转换后的值是否相等,忽略 变量类型,如:'1' == 1 //true 3.=== : 比较运算,比较值和变量类型是否相等,如:'1' ...
- GIT GUI克隆github代码
新建一个文件夹,右击gitgui git clone 去掉不要
- yii2 使用gii生成代码文件
访问地址: http://localhost/yii2-test/web/index.php?r=gii 如果你通过本机以外的机器访问 Gii,请求会被出于安全原因拒绝. 在web.php修改gii ...