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 ...
随机推荐
- 【Data structure & Algorithm】把二元查找树转变成排序的双向链表
把二元查找树转变成排序的双向链表 题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表,要求不能创建任何新节点,只调整指针指向. 比如将二元查找树 10 / \ 6 ...
- 2019Unite大会
一年一度的Unite大会,在今年(2019)的5月10日准时在上海的国际会议中心盛大举行.本届大会历时三天,由来自全球的Unity公司技术专家.开发者.艺术家和Unity爱好者们齐聚一堂,展示Unit ...
- Swift3.0 轮播图
使用三个UIButton实现无限轮播:https://github.com/LXfeiYu/LXCarouselImages.git 喜欢的朋友给个星!!! 功能: 1.可以选择开启和关闭定时器 2. ...
- PhpStorm插件之Translation
安装插件 File->Setting->Pluugins 搜索 Translation 如何使用 安装完插件后,RESTART IDE 快捷键 Ctrl+Shift+Y OR 选 ...
- Unity5.0 EventSystem事件系统的详细说明
http://www.manew.com/blog-56596-2917.html?_dsign=53b25d5d unity5.0开发了新的UI系统UGUI,附带的事件系统EventSystem不仅 ...
- 51nod1770(xjb)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1770 题意:中文题诶- 思路:随便写几个例子不难发现乘机中间部 ...
- 洛谷 P1712 [NOI2016]区间(线段树)
传送门 考虑将所有的区间按长度排序 考虑怎么判断点被多少区间覆盖,这个可以离散化之后用一棵权值线段树来搞 然后维护两个指针$l,r$,当被覆盖次数最多的点的覆盖次数小于$m$时不断右移$r$,在覆盖次 ...
- JS中的柯里化(currying) 转载自张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com]
JS中的柯里化(currying) by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpr ...
- ADO学途 five day 连接数据库
用一个程序的目的就是为了方便对数据进行操作,没有数据的支持,程 序就成了一个空壳子.一般我们常用的数据库有三种mysql, SQL server, Oracle. C#中常用的就是SQL server ...
- dbutils下载