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...仔细想 ...
随机推荐
- OLTP 与 OLAP
OLTP:On-Line Transaction Processing(联机事务处理过程).也称为面向交易的处理过程,其基本特征是前台接收的用户数据可以立即传送到计算中心进行处理,并在很短的时间内给出 ...
- 【DNN 系列】 DNN是什么
DNN平台 这个DNN平台是一个开放的.可扩展的.安全的.可扩展的内容管理系统和ASP.NET.世界各地的数十万商户,从地方小企业到全球1000强企业,取决于DNN平台作为他们网站的编辑环境. 这个网 ...
- Android——PullToRefresh自动刷新
需求:强制刷新 方法一: PullToRefreshListView本身提供了一个setRefreshing()接口,调用该接口会自动触发下拉刷新的操作(前提是支持下拉刷新).按照一般的操作我们直接在 ...
- c# 之抽象属性
抽象属性 属性可以使类.结构.接口的成员,自然也可以是抽象类的抽象属性了,抽象属性同抽象方法一样在派生类中被实现. using System; using System.Collections.Gen ...
- python 自动广播机制 (broadcasting)
一定要注意,执行 broadcast 的前提在于,两个 ndarray 执行的是 element-wise(按位加,按位减) 的运算,而不是矩阵乘法的运算,矩阵乘法运算时需要维度之间严格匹配.(且矩阵 ...
- [NOIP2014提高组]联合权值
题目:洛谷P1351.Vijos P1906.codevs3728.UOJ#16. 题目大意:有一个无向连通图,有n个点n-1条边,每个点有一个权值$W_i$,每条边长度为1.规定两个距离为2的点i和 ...
- 紫书 例题 10-21 UVa 11971(连续概率)
感觉这道题的转换真的是神来之笔 把木条转换成圆,只是切得次数变多一次 然后只要有一根木条长度为直径就租不成 其他点的概率为1/2^k 当前这个点的有k+1种可能 所以答案为1 - (k+1)/2^k ...
- Oracle拾遗
这次学习Oracle视频.还是学到了不少东西的. 首先,这是一次系统的学习.对自己的知识体系是一次查漏补缺,曾经仅仅是简单的会用,如今看到出的问题,非常easy就能够想到是哪一部分出了问题.尤其是如今 ...
- An internal error occurred during: "Checking tomcat state". Error while reading server.xml
An internal error occurred during: "Checking tomcat state". Error while reading server.xml ...
- linux系统 硬链接和软链接
背景: 当几个用户同在一个项目里工作时.经常须要共享文件. 假设一个共享文件同一时候出如今属于不同用户的不同文件夹下.工作起来就非常方便. 比如B和C文件夹下有一文件D是两者都能够訪问和改动的共享文件 ...