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.

Sample test(s)
input
3 2
1 2 4
output
1
input
5 2
3 -5 3 -5 3
output
0
input
6 3
4 3 4 3 2 5
output
3
Note

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.

题目链接:点击打开链接

题目大意:给出n个数。给出一个k值。问n个数要怎么排列能够使的值最小。

首先我们能够想到,一段连续的数在a[i],a[i+k],a[i+2*k],。,那么对于这些数来说他们的差的和是最小的。

所以先对n个数排序。然后将这n个数分成k段,k段中有k-n%k段的长度是n/k。n%k段的长度是n/k+1,这些段刚好能够填满n个数的序列,这n个数的总的差是a[n]-a[1],当中有k-1个断点。是不会被统计到的,所以问题就转化成了怎样分开序列,使得分成要求的段的个数和段的长度,并且使断点的和最大,这样就会保证终于的结果最小。

dp[i][j]从头開始分出了i段成都为n/k的。j段n/k+1的断点的最大值。那么状态转移方程:

dp[i][j] = max(dp[i][j],dp[i-1][j]+a[k+1]-a[k]) ;

dp[i][j] = max(dp[i][j],dp[i][j-1]+a[k+1]-a[k]) ;

终于a[n]-a[1]-dp[num0][num1]

注意当k大于n的时候,结果是0

#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std ;
#define LL __int64
LL a[300100] , sum[300100];
LL dp[5010][5010] ;
int main() {
int n , k , i , j , l0 , l1 , num1 , num0 ;
LL max1 ;
while( scanf("%d %d", &n, &k) != EOF ) {
sum[0] = 0 ;
for(i = 1 ; i <= n ; i++) {
scanf("%I64d", &a[i]) ;
sum[i] = a[i] + sum[i-1] ;
}
sort(a+1,a+n+1) ;
if( n <= k ) {
printf("0\n") ;
continue ;
}
memset(dp,0,sizeof(dp)) ;
l0 = n/k ;
l1 = n/k+1 ;
num0 = k-n%k ;
num1 = n%k ;
a[0] = a[1] ;
for(i = 0 ; i <= num0 ; i++) {
for(j = 0 ; j <= num1 ; j++) {
if( i == 0 && j == 0 ) continue ;
if( i ) {
k = (i-1)*l0 + j*l1 ;
dp[i][j] = max(dp[i][j],dp[i-1][j]+a[k+1]-a[k]) ;
}
if( j ) {
k = i*l0 + (j-1)*l1 ;
dp[i][j] = max(dp[i][j],dp[i][j-1]+a[k+1]-a[k]) ;
}
}
}
printf("%I64d\n", a[n]-a[1]-dp[num0][num1]) ; }
return 0 ;
}

codeforces 571B--Minimization(贪心+dp)的更多相关文章

  1. Codeforces 571B Minimization:dp + 贪心【前后相消】

    题目链接:http://codeforces.com/problemset/problem/571/B 题意: 给你一个长度为n的数列a[i]. 现在你可以随意改变数字的位置,问你 ∑| a[i] - ...

  2. CodeForces - 940E - Cashback +贪心+DP

    传送门:CodeForces - 940E - Cashback 题意:在一个长度为n的数组中,可以分出长度为 k 连续的多个数组b(每个数组 b 的 k 可不相同),然后,可以对每个数组 b 进行删 ...

  3. [CF571B]Minimization(贪心+DP)

    题目链接 http://codeforces.com/problemset/problem/571/B 题意 给数组,得到公式最小值. 题解 由题分成的子数组只有两种长度,每种长度的数组数量也是固定的 ...

  4. Codeforces 571B Minimization

    http://codeforces.com/problemset/problem/571/B 给出一个序列,可以任意调整序列的顺序,使得给出的式子的值最小 思路:我们可以把序列分解,变成k条链,n%k ...

  5. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

  6. 【BZOJ-3174】拯救小矮人 贪心 + DP

    3174: [Tjoi2013]拯救小矮人 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 686  Solved: 357[Submit][Status ...

  7. BZOJ_3174_[Tjoi2013]拯救小矮人_贪心+DP

    BZOJ_3174_[Tjoi2013]拯救小矮人_贪心+DP Description 一群小矮人掉进了一个很深的陷阱里,由于太矮爬不上来,于是他们决定搭一个人梯.即:一个小矮人站在另一小矮人的 肩膀 ...

  8. 洛谷P4823 拯救小矮人 [TJOI2013] 贪心+dp

    正解:贪心+dp 解题报告: 传送门! 我以前好像碰到过这题的说,,,有可能是做过类似的题qwq? 首先考虑这种显然是dp?就f[i][j]:决策到了地i个人,跑了j个的最大高度,不断更新j的上限就得 ...

  9. 【bzoj5073】[Lydsy1710月赛]小A的咒语 后缀数组+倍增RMQ+贪心+dp

    题目描述 给出 $A$ 串和 $B$ 串,从 $A$ 串中选出至多 $x$ 个互不重合的段,使得它们按照原顺序拼接后能够得到 $B$ 串.求是否可行.多组数据. $T\le 10$ ,$|A|,|B| ...

随机推荐

  1. mysql 编码错误修改

    set character_set_results=utf8;

  2. CMDB学习之四 ——DEBUG模式

    定义一个debug,进行解析调试,到测试文件 配置文件,配置debug模式,定义环境变量, #!/usr/bin/env python # -*- coding:utf-8 -*- import os ...

  3. ZJU 2425 Inversion

    Inversion Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID:  ...

  4. smarty模板引擎(一)基础知识

    一.基本概念 1.什么是mvc?     mvc是一种开发模式,核心思想是:数据的输入.数据的处理.数据显示的强制分离. 2.什么是smarty?     smarty是一个php的模板引擎.更明白的 ...

  5. [NowCoder]牛客网NOIP赛前集训营-提高组(第六场)题解

    A.最长路 题意:给定有向图,每条边有个字符\([0,10^9]\),求每个点最长路字典序最小的方案.\(N,M\le 10^6\) 建反图跑拓扑排序,显然入过队的点都有最长路,考虑如何判断字典序大小 ...

  6. Linux系统捕获数据包流程

    Linux系统捕获数据包流程 为了提高数据包的捕获效率,瓶颈问题是一个需要非常关注的焦点.减少在捕获数据包过程中的瓶颈,就能够提高数据包捕获的整体性能.下面本文将以Linux操作系统为平台,分析捕获数 ...

  7. Trie图(模板)

    Trie图(蒟蒻听说AC自动机能做的题Trie图都能做,而且AC自动机可能被卡,就没学过AC自动机),最近想捡一捡,好久之前做的了. Trie图,就是一个在Trie树上建的图  大概描述一下 比如说有 ...

  8. ListCtrl添加右键菜单(在对话框类中)

    在对话框类中如何添加NM_RCLICK消息: ListCtrl控件右键单击选择属性 在右侧属性栏中选择控件事件 在控件事件中找到NM_RCLICK并添加 完成,写代码

  9. 洛谷 P1724 东风早谷苗

    洛谷 P1724 东风早谷苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走, ...

  10. [Vue + TS] Create Type-Safe Vue Directives in TypeScript

    Directives allow us to apply DOM manipulations as side effects. We’ll show you how you can create yo ...