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+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的更多相关文章
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Order Book 模拟
原题链接:http://codeforces.com/contest/572/problem/B 题意 很迷,自行看题. 题解 看懂题就会做了 代码 #include<iostream> ...
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Array 模拟
题目链接:http://codeforces.com/contest/572/problem/A 题意 就给你两个数组,问你能不能从A数组中取出k个,B数组中取出m个,使得这k个都大于这m个. 题解 ...
- Codeforces Round #539Ȟȟȡ (Div. 1) 简要题解
Codeforces Round #539 (Div. 1) A. Sasha and a Bit of Relax description 给一个序列\(a_i\),求有多少长度为偶数的区间\([l ...
- 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 ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题解
Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2) 题目链接:https://codeforces.com/contest/1130 ...
- Codeforces Round #317 (div 2)
Problem A Arrays 思路:水一水. #include<bits/stdc++.h> using namespace std; ; int n1,n2,k,m,a[N],b[N ...
- Codeforces Round #317 (Div. 2) D Minimization (贪心+dp)
D. Minimization time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #317 (Div. 2) C Lengthening Sticks (组合,数学)
一个合法的三角形的充要条件是a<b+c,其中a为最长的一边,可以考虑找出所有不满足的情况然后用总方案减去不合法的情况. 对于一个给定的总长度tl(一定要分完,因为是枚举tl,不分配的长度已经考虑 ...
- 严格递增类的dp Codeforces Round #371 (Div. 1) C dp
http://codeforces.com/contest/713 题目大意:给你一个长度为n的数组,每次有+1和-1操作,在该操作下把该数组变成严格递增所需要的最小修改值是多少 思路:遇到这类题型, ...
随机推荐
- LeetCode(282) Peeking Iterator
题目 Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peek ...
- hdu-2553 N皇后问题(搜索题)
在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合法的放置方法. Inpu ...
- 彻底卸载gedit
$ sudo apt-get purge gedit gedit-plugins$ sudo apt-get autoremove
- selenium2-元素管理方式及解析
1.管理文件格式:yaml 2.Yaml里面的内容格式: 3.格式说明: baidu_input后面接上":",直接回车,然后空两格 type与value这两个key是固定 ...
- luogu2765 魔术球问题
发现好像没人来证明贪心啊--那我来写一下它的证明 欲证明:放一个数在已有的柱上(如果可以)总是比新开一个柱更优的 假如已经放了x1..x2....xu..xv..xw.... 现在我要放xx 我有两种 ...
- 关于ios 和 android 录音(语音)对聊文件格式问题
关于ios 和 android 录音(语音)对聊文件格式问题 在做语音对讲的时候,将会碰到录制语音格式的问题,这些需要跨平台我们可能需要使用双方平台都支持的格式,或者执行编码转换 解决方式如下: wa ...
- 静态方法中不能使用 $this
忽略了一个问题,$this 代表当前对象!! 静态方法中应该使用 类名 . self 或者 static 关键字来代替! static public function get_info($id) { ...
- web自动化之selenium
一.Selenium(http://www.selenium.org/) Web自动化测试工具.它支持各种浏览器,包括Chrome,Safari,Firefox等主流界面式浏览器,如果你在这些浏览器里 ...
- Python字典类型、
字典类型: # msg_dic = {# 'apple': 10,# 'tesla': 100000,# 'mac': 3000,# 'lenovo': 30000,# ...
- javascript学习笔记 - 引用类型 Array
二 Array 1.可以通过length属性删除或创建新的数组项 arr = [1,2,3]; arr.length = 4;//增加 [1,2,3,undefined] arr.length = 2 ...