原题链接:http://codeforces.com/contest/572/problem/D

题意

给你个数组A和n,k,问你排列A后,下面的最小值是多少。

题解

先排个序,要填充像1,1+k,1+2k,1+3k....这样的序列,或像2,2+k,2+2k.......这样的序列,这些序列应该取排序数组中连续的一段才能使得答案最小,现在考察这些序列的大小,发现其大小要么是n/k,要么是n/k+1,所以可以dp[i][j]表示前 i 条序列我取了 j 个n/k这样的序列。转移就很简单了,详见代码。

代码

#include <bits/stdc++.h>

using namespace std;

long long dp[][];
int n, k, a, b, x, y, z[]; long long inf = 1e18; int main() {
//freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
while (scanf("%d %d", &n, &k) != EOF) {
for (int i = ; i <= n; i++)
scanf("%d", &z[i]);
sort(z + , z + n + );
x = n / k;
y = n / k + ;
b = n - k * x;
a = k - b;
dp[][] = z[y] - z[];
dp[][] = z[x] - z[];
for (int i = ; i <= a + b; i++) {
for (int j = ; j <= min(a, i); j++) {
if (i == j) {
dp[i][j] = dp[i - ][j - ] + (long long)z[(i - ) * x + x] - z[(i - ) * x + ];
}
else {
int tmp = j * x + (i - - j) * y;
if (tmp + y <= n) dp[i][j] = dp[i - ][j] + (long long)z[tmp + y] - z[tmp + ];
else dp[i][j] = inf;
if (j > ) {
tmp = (j - ) * x + (i - j) * y;
if (tmp + x <= n)
dp[i][j] = min(dp[i][j], dp[i - ][j - ] + (long long)z[tmp + x] - z[tmp + ]);
}
}
}
}
//for (int i = 1; i <= 3; i++) printf("--> %I64d\n", dp[i][i]);
printf("%I64d\n", dp[a + b][a]);
}
return ;
}

Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Minimization dp的更多相关文章

  1. Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Order Book 模拟

    原题链接:http://codeforces.com/contest/572/problem/B 题意 很迷,自行看题. 题解 看懂题就会做了 代码 #include<iostream> ...

  2. Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Array 模拟

    题目链接:http://codeforces.com/contest/572/problem/A 题意 就给你两个数组,问你能不能从A数组中取出k个,B数组中取出m个,使得这k个都大于这m个. 题解 ...

  3. Codeforces Round #539&#542&#543&#545 (Div. 1) 简要题解

    Codeforces Round #539 (Div. 1) A. Sasha and a Bit of Relax description 给一个序列\(a_i\),求有多少长度为偶数的区间\([l ...

  4. Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!

    VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM Time Lim ...

  5. Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题解

    Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题目链接:https://codeforces.com/contest/1130 ...

  6. Codeforces Round #317 (div 2)

    Problem A Arrays 思路:水一水. #include<bits/stdc++.h> using namespace std; ; int n1,n2,k,m,a[N],b[N ...

  7. Codeforces Round #317 (Div. 2) D Minimization (贪心+dp)

    D. Minimization time limit per test  2 seconds memory limit per test  256 megabytes input  standard ...

  8. Codeforces Round #317 (Div. 2) C Lengthening Sticks (组合,数学)

    一个合法的三角形的充要条件是a<b+c,其中a为最长的一边,可以考虑找出所有不满足的情况然后用总方案减去不合法的情况. 对于一个给定的总长度tl(一定要分完,因为是枚举tl,不分配的长度已经考虑 ...

  9. 严格递增类的dp Codeforces Round #371 (Div. 1) C dp

    http://codeforces.com/contest/713 题目大意:给你一个长度为n的数组,每次有+1和-1操作,在该操作下把该数组变成严格递增所需要的最小修改值是多少 思路:遇到这类题型, ...

随机推荐

  1. python 数据结构与算法之排序(冒泡,选择,插入)

    目录 数据结构与算法之排序(冒泡,选择,插入) 为什么学习数据结构与算法: 数据结构与算法: 算法: 数据结构 冒泡排序法 选择排序法 插入排序法 数据结构与算法之排序(冒泡,选择,插入) 为什么学习 ...

  2. leetcode-15-basic-string

    58. Length of Last Word 解题思路: 从结尾向前搜索,空格之前的就是最后一个词了.写的时候我考虑了尾部有空格的情况.需要注意的是,测试用例中有" "的情况,此 ...

  3. LeetCode(168) Excel Sheet Column Title

    题目 Given a positive integer, return its corresponding column title as appear in an Excel sheet. For ...

  4. HDU - 6199 gems gems gems (DP)

    有n(2e4)个宝石两个人轮流从左侧取宝石,Alice先手,首轮取1个或2个宝石,如果上一轮取了k个宝石,则这一轮只能取k或k+1个宝石.一旦不能再取宝石就结束.双方都希望自己拿到的宝石数比对方尽可能 ...

  5. hdu 6301

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. 2015四川省赛 D Vertex Cover 搜索

    题意: 给出一个\(n\)个点\(m\)条边的无向图,现在要给若干个点染色,使得每条边都至少邻接一个被染色的顶点.问至少要给多少各点染色才能满足条件. 分析: 注意到题目中有一个很特殊的条件: 对于图 ...

  7. ogre3D学习基础6---场景管理器的使用

    场景管理器的使用 最常使用的坐标系统空间(同时也是Ogre程序所能提供的)即是世界空间(World).父节点空间(Parent)以及本地空间(Local). 1.世界空间 就是物体所存在的地方,当我们 ...

  8. 十分钟了解socket

    socket通讯方式------socket是TCP/IP协议的网络数据通讯接口(一种底层的通讯的方式).socket是IP地址和端口号的组合.例如:192.168.1.100:8080 原理就是TC ...

  9. bootstrap里的fileimput的小问题

    fileinput 是bootstrap 里面一个非常好的插件 于是我很开心的开始的使用了 $("#file_upload").fileinput({ uploadUrl: &qu ...

  10. 【转载】用OCTAVE实现一元线性回归的梯度下降算法

    原文地址:http://www.cnblogs.com/KID-XiaoYuan/p/7247481.html STEP1 PLOTTING THE DATA 在处理数据之前,我们通常要了解数据,对于 ...