原题链接: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里字典的用法介绍

    一.什么是字典 字典是python里的一种数据类型,特点是元素的无序性,和键key的唯一性.字典的创建方法是{key:values},字典里的键key只能是不可变的数据类型(整型,字符串或者是元组), ...

  2. CentOS7.2下Hadoop2.7.2的集群搭建

    1.基本环境: 操作系统: Centos 7.2.1511 三台虚机: 192.168.163.224  master 192.168.163.225  node1 192.168.163.226   ...

  3. 并查集:POJ1182-食物链(并查集比较高端的应用)

    食物链 Time Limit: 1000MS Memory Limit: 10000K Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C ...

  4. meteor 检测运行环境,手机或者桌面

    meteor add mystor:device-detection Meteor.Device.isPhone() https://atmospherejs.com/mystor/device-de ...

  5. Selenium WebDriver-操作下拉框内容

    操作下拉框中的内容 #encoding=utf-8 import unittest import time import chardet from selenium import webdriver ...

  6. mvc “System.NullReferenceException”类型的异常在 App_Web_zo44wdaq.dll 中发生,但未在用户代码中进行处理 其他信息: 未将对象引用设置到对象的实例。

    “System.NullReferenceException”类型的异常在 App_Web_zo44wdaq.dll 中发生,但未在用户代码中进行处理 其他信息: 未将对象引用设置到对象的实例. 解决 ...

  7. Map容器——HashMap及常用API,及put,get方法解析,哈希码的产生和使用

    Map接口 ①   映射(map)是一个存储键/值对的对象.给定一个键,可以查询到它的值,键和值都是对象; ②   键必须是唯一的,值可以重复; ③   有些映射可以接收null键和null值,而有的 ...

  8. Linux下安装Mysql出现的常见问题以及解决办法

     1.安装时候出现 warning: mysql-community-server-5.7.13-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ...

  9. 【bzoj2333】[SCOI2011]棘手的操作 可并堆+STL-set

    UPD:复杂度是fake的...大家还是去写启发式合并吧. 题目描述 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条 ...

  10. BZOJ 4817 [Sdoi2017]树点涂色 ——LCT 线段树

    同BZOJ3779. SDOI出原题,还是弱化版的. 吃枣药丸 #include <map> #include <cmath> #include <queue> # ...