题意翻译

给定数组AAA 和值kkk ,你可以重排AAA 中的元素,使得∑i=1n−k∣Ai−Ai+k∣\displaystyle\sum_{i=1}^{n-k} |A_i-A_{i+k}|i=1∑n−k​∣Ai​−Ai+k​∣ 最小。输出最小值。
题目描述

You've got array A A A , consisting of n n n integers and a positive integer k k k . Array A A A is indexed by integers from 1 1 1 to n n 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 n,k n,k ( 2<=n<=3⋅105 2<=n<=3·10^{5} 2<=n<=3⋅105 , 1<=k<=min(5000,n−1) 1<=k<=min(5000,n-1) 1<=k<=min(5000,n−1) ).

The second line contains n n n integers A[1],A[2],...,A[n] A[1],A[2],...,A[n] A[1],A[2],...,A[n] ( −109<=A[i]<=109 -10^{9}<=A[i]<=10^{9} −109<=A[i]<=109 ), separate by spaces — elements of the array A A A .

输出格式:

Print the minimum possible value of the sum described in the statement.

输入输出样例
输入样例#1: 复制

3 2
1 2 4

输出样例#1: 复制

1

输入样例#2: 复制

5 2
3 -5 3 -5 3

输出样例#2: 复制

0

输入样例#3: 复制

6 3
4 3 4 3 2 5

输出样例#3: 复制

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 $ .

题解:首先显然如果把i,i+k,i+2×k等排列起来,这将是一条链,而且会有k条链。

链长可能有两种情况:n/k,n/k+1

显然同一条链中如果数字固定,单调的话链的贡献最小

显然如果单调的话,取连续的几个贡献最小

所以我们现将全部数字放到同一条链里,接着将这条长链断成k条长度为n/k,n/k+1的链,此时贡献需要减掉两条链之间的差值

于是问题转化成了求减掉差值的和的最大值

令i表示长边数量,j表示短边的数量

dp[i][j]=max(dp[i][j-1]+det1,dp[i-1][j]+det2)

因为只有两种边长,所以分割点所在的位置可以直接根据i和j推出

总贡献减去dp[cnt1][cnt2]即为答案

 

代码如下:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #define int long long
  6. using namespace std;
  7. int n,k,a[];
  8. int dp[][];
  9. int ans;
  10. signed main()
  11. {
  12. scanf("%lld%lld",&n,&k);
  13. int cnt1=n%k;
  14. int cnt2=k-cnt1;
  15. int len1=n/k+;
  16. int len2=n/k;
  17. for(int i=;i<=n;i++)
  18. {
  19. scanf("%lld",&a[i]);
  20. }
  21. sort(a+,a+n+);
  22. for(int i=;i<=n;i++)
  23. {
  24. ans+=a[i]-a[i-];
  25. }
  26. for(int i=;i<=cnt1;i++)
  27. {
  28. dp[i][]=dp[i-][]+a[i*len1-len1+]-a[i*len1-len1];
  29. }
  30. for(int i=;i<=cnt2;i++)
  31. {
  32. dp[][i]=dp[][i-]+a[i*len2-len2+]-a[i*len2-len2];
  33. }
  34. for(int i=;i<=cnt1;i++)
  35. {
  36. for(int j=;j<=cnt2;j++)
  37. {
  38. dp[i][j]=max(dp[i][j-]+a[i*len1+j*len2-len2+]-a[i*len1+j*len2-len2],dp[i-][j]+a[i*len1+j*len2-len1+]-a[i*len1+j*len2-len1]);
  39. }
  40. }
  41. printf("%lld",ans-dp[cnt1][cnt2]);
  42. }

CodeForces 572D Minimization(DP)的更多相关文章

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

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

  2. Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Minimization dp

    原题链接:http://codeforces.com/contest/572/problem/D 题意 给你个数组A和n,k,问你排列A后,下面的最小值是多少. 题解 先排个序,要填充像1,1+k,1 ...

  3. codeforces 682D(DP)

    题目链接:http://codeforces.com/contest/682/problem/D 思路:dp[i][j][l][0]表示a串前i和b串前j利用a[i] == b[j]所得到的最长子序列 ...

  4. codeforces 666A (DP)

    题目链接:http://codeforces.com/problemset/problem/666/A 思路:dp[i][0]表示第a[i-1]~a[i]组成的字符串是否可行,dp[i][1]表示第a ...

  5. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

  6. Codeforces 55D (数位DP+离散化+数论)

    题目链接: http://poj.org/problem?id=2117 题目大意:统计一个范围内数的个数,要求该数能被各位上的数整除.范围2^64. 解题思路: 一开始SB地开了10维数组记录情况. ...

  7. Codeforces 264B 数论+DP

    题目链接:http://codeforces.com/problemset/problem/264/B 代码: #include<cstdio> #include<iostream& ...

  8. Codeforces 571B Minimization

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

  9. CodeForces 398B 概率DP 记忆化搜索

    题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...

随机推荐

  1. 【android】SDK在线升级

    1.修改本地hosts文件 hosts文件位置:C:\Windows\System32\drivers\etc\hosts 在底部添加:203.208.46.146 dl-ssl.google.com ...

  2. 闲扯淡笔记 - Web的历史

    这里的Web指的是万维网,就是World Wide Web. 文档和静态资源 通过URL组织 Tim Berners Lee (TimBL) 于1989发明这个概念,这丫55年出生,和我父亲一般大. ...

  3. OpenSSL生成root CA及签发证书

    一.openssl 简介 openssl 是目前最流行的 SSL 密码库工具,其提供了一个通用.健壮.功能完备的工具套件,用以支持SSL/TLS 协议的实现.官网:https://www.openss ...

  4. undefined和NAN的区别(转)

    Javascript 中 null.NaN和undefined的区别 1.类型分析: js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始 ...

  5. 2017CCSP总结——失败(铜)

    这次比赛,算是铩羽而归.尽管是第一次出去打比赛,在经验方面略显不足,但是,归根到底,我这次比赛打的很失败.包括我们学校去的,打的也不好,可以说是全体翻车.真的很对不起带我们去的老师.>_< ...

  6. C++和C# WebService相互调用

    C#调用C++ gSOAP: 调用http://blog.csdn.net/ggz631047367/article/details/44567411的服务http://127.0.0.1:8089/ ...

  7. 公共文件模块include

    首先新建一个include 把所有引入的文件放入公共文件里 function getBaseURL() { var pathName = window.document.location.pathna ...

  8. maven常用命令集合

    作者:ydlmlh 原文:http://ydlmlh.iteye.com/blog/2158973 抽了点时间,整理了一些maven常用命令参数,以便参考:参考了maven官网和网上其他一些maven ...

  9. ubuntu apt-get 代理

    # cat /etc/apt/apt.conf Acquire::http::Proxy "http://109.105.4.17:8119"; Acquire::https::P ...

  10. 手游热更新方案xLua开源:Unity3D下Lua编程解决方案

    C#下Lua编程支持 xLua为Unity. .Net. Mono等C#环境增加Lua脚本编程的能力,借助xLua,这些Lua代码可以方便的和C#相互调用. xLua的突破 xLua在功能.性能.易用 ...