原题链接: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. 04 Django模板

    基本概念 作为Web框架,Django提供了模板,用于编写html代码,还可以嵌入模板代码更快更方便的完成页面开发,再通过在视图中渲染模板,将生成最终的html字符串返回给客户端浏览器 模版致力于表达 ...

  2. ARM系统调用

    参考:Linux异常处理体系结构 linux系统调用表(system call table)  Arm Linux系统调用流程详细解析-SWI ARM系统调用是通过SWI异常处理函数实现的,这里简要概 ...

  3. py文件转exe时包含paramiko模块出错解决方法

    问题描述:python代码中包含paramiko模块的远程登录ssh,在用pyInstaller转为exe时报错, 报错提示为“No handlers could be found for logge ...

  4. UOJ 152 汉诺塔 分治

    题目链接 题意: 有三根编号为\((1, \, 2, \, 3)\)的柱子,然后第一根柱子上有编号为\(1 \sim n(n \leq 10000)\)的盘子,从上到下第\(i\)个盘子的编号是\(A ...

  5. HashMap的C++实现

    #include <iostream> #include <cstring> #include <string> typedef unsigned int SIZE ...

  6. day06 面向对象编程

    面向对象: 面向对象: 世界万物,皆可分类 世界万物,皆为对象   只要是对象,就肯定属于某种品类 只要是对象,就肯定有属性         特性: 多态: 一个统一的接口,多种实现  例如:  一个 ...

  7. 《Python全栈开发指南》第3版 Alex著(LFXC2018)

    第一章 Python基础——Python介绍&循环语句 1.1 编程语言介绍 1.2 Python介绍 1.3 Python安装 1.4 第一个Python程序 1.5 变量 1.6 程序交互 ...

  8. 01-python进阶-拾遗

    列表复习append(x)追交到链尾extend(L)追加一个列表 等价于 +=insert(i,x)在位置i处插入xremove(x) 删除一个值为x的元素 如果没有抛出异常sort() 直接修改列 ...

  9. 解决Failed with error: unable to access 'https://git.coding.net/chenmi1234/lianpos.git/': Couldn't resolve host 'git.coding.net'

    代码改变世界 github push 出现问题 Failed with error: unable to access 'https://git.coding.net/chenmi1234/lianp ...

  10. ambari单节点集群塔建

    配置2台机器,发别为ambari01.ambari03.ambari01上部署Ambari-server和Mirror server,另一台机器上部署agent. 一.配置静态IP 运行命令,让配置生 ...