codeforce 571 B Minimization
题意:给出一个序列,经过合适的排序后。使得
最小。
做法:将a升序排序后,dp[i][j]:选择i个数量为n/k的集合,选择j个数量为n/k+1的集合的最小值。
举个样例,
a={1,2,3,4,5,6,7,8,9,10},k=2
那么直接贪心可做,是这样。
1,x,2,x,3,x,4,x,5,x。(也就是1,2,3,4,5作为一个集合)
6 7 8 9 10(也就是6,7,8,9,10作为一个集合)
放在一起就是1,6。2。7。3,8,4。9,5,10。
若是k=3就要考虑长度为n/k+1=4的集合,是将1,2,3,4放在一起呢?还是4。5。6,7放在一起呢?就须要dp了。
dp[i+1][j]=min(dp[i+1][j],dp[i][j]+sb[x+sz-1]-sb[x]);
dp[i][j+1]=min(dp[i][j+1],dp[i][j]+sb[x+sz]-sb[x]);
x:当前还未考虑元素的最小下标
sb:一段连续元素差的和
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
int a[300010];
ll sb[3000010];
ll dp[5010][5010];
int main()
{
int n,k;
cin>>n>>k;
int sz=n/k;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
for(int i=1;i<n;i++)
sb[i]=sb[i-1]+a[i]-a[i-1];
memset(dp,63,sizeof(dp));
dp[0][0]=0;
int m=n%k,len=k-m;
for(int i=0;i<=len;i++)
for(int j=0;j<=m;j++)
{
int x=(i+j)*sz+j;
dp[i+1][j]=min(dp[i+1][j],dp[i][j]+sb[x+sz-1]-sb[x]);
dp[i][j+1]=min(dp[i][j+1],dp[i][j]+sb[x+sz]-sb[x]);
}
cout<<dp[len][m];
}
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.
3 2
1 2 4
1
5 2
3 -5 3 -5 3
0
6 3
4 3 4 3 2 5
3
In the first test one of the optimal permutations is 1 4 2.
In the second test the initial order is optimal.
In the third test one of the optimal permutations is 2 3 4 4 3 5.
codeforce 571 B Minimization的更多相关文章
- 三维网格去噪算法(L0 Minimization)
[He et al. 2013]文章提出了一种基于L0范数最小化的三角网格去噪算法.该思想最初是由[Xu et al. 2011]提出并应用于图像平滑,假设c为图像像素的颜色向量,▽c为颜色向量的梯度 ...
- Codeforce - Street Lamps
Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...
- Codeforce Round #216 Div2
e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...
- 571亿背后:DRC助阿里实现异地双活
571亿背后:DRC助阿里实现异地双活 赶集网SQL自动上线
- Codeforces 571B Minimization
http://codeforces.com/problemset/problem/571/B 给出一个序列,可以任意调整序列的顺序,使得给出的式子的值最小 思路:我们可以把序列分解,变成k条链,n%k ...
- depcomp: line 571: exec: g++: not found
../depcomp: line 571: exec: g++: not foundmake[1]: *** [my_new.o] Error 127make[1]: Leaving director ...
- Codeforce 水题报告(2)
又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...
- codeforce 375_2_b_c
codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...
- codeforce 367dev2_c dp
codeforce 367dev2_c dp 标签: dp 题意: 你可以通过反转任意字符串,使得所给的所有字符串排列顺序为字典序,每次反转都有一定的代价,问你最小的代价 题解:水水的dp...仔细想 ...
随机推荐
- Mysql常见更改密码方法
ERROR (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) ch ...
- VS2012恢复默认设置的2种方法
方法一: 工具 → 导入和导出设置 → 重置所有设置 → 下一步 → 选择“是否保存当前设置”,下一步 → 选择“要重置的开发语言(如,Visual C# 开发设置)” → 完成. 方法二: 1.依次 ...
- CDH5.14操作指南
1.Cdh 5.14介绍 2.版本发布概要 3.安装要求 4.Cloudera Manager介绍 5.Cloudera 安装 6.Cloudera 管理 7.Cloudera Navigator C ...
- POJ 3539 Elevator(同余类BFS)
题意 有一部电梯,最初停在1层. 电梯有4个按键,上升a,b,c层,回到一层. 求从一层出发.能到达1~h的哪些楼层. (h<=1018,a,b,c<=105) 题解 这种h能大的图论,一 ...
- [洛谷P1156][codevs1684]垃圾陷阱
题目大意:一头奶牛掉进了深度为d的坑里,现在有g个垃圾在特定时刻ti扔进来.奶牛可以吃垃圾以获得体力,吃第i个垃圾能获得mi的体力,也可以堆放垃圾以逃生,第i个垃圾高度为hi.当高度≥d时奶牛成功逃生 ...
- Win10 + YOLOv3 环境配置,编译,实现目标检测----How to compile YOLOv3 on Windows
其他比较好的参考链接: 环境配置: 环境配置的最终图片列表:https://blog.csdn.net/shanglianlm/article/details/80322718 视频讲解YOLOv1: ...
- MySQL auttoReconnect
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from ...
- COGS——T 8. 备用交换机
http://www.cogs.pro/cogs/problem/problem.php?pid=8 ★★ 输入文件:gd.in 输出文件:gd.out 简单对比时间限制:1 s 内存 ...
- Android输入法扩展之远程输入法
近年来,互联网电视開始火热,乐视TV,小米TV,近期爱奇艺也在大肆的招人做爱奇艺电视.当然还有更被关注的苹果电视.事实上,这个趋势非常正常,也非常合理,传统单纯的接收电视节目的电视已经太传统了.是该被 ...
- unix mkdir命令的使用方法
[语法]: mkdir [-m 模式] [-p] 文件夹名 [说明]: 本命令用于建立文件夹,文件夹的存取模式由掩码(umask)决定,要求对其父文件夹具有写权限,文件夹的UID和GID为实际 ...