题目链接:https://vjudge.net/problem/HDU-3480

Division

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others)
Total Submission(s): 5304    Accepted Submission(s): 2093

Problem Description
Little D is really interested in the theorem of sets recently. There’s a problem that confused him a long time.  
Let T be a set of integers. Let the MIN be the minimum integer in T and MAX be the maximum, then the cost of set T if defined as (MAX – MIN)^2. Now given an integer set S, we want to find out M subsets S1, S2, …, SM of S, such that

and the total cost of each subset is minimal.

 
Input
The input contains multiple test cases.
In the first line of the input there’s an integer T which is the number of test cases. Then the description of T test cases will be given. 
For any test case, the first line contains two integers N (≤ 10,000) and M (≤ 5,000). N is the number of elements in S (may be duplicated). M is the number of subsets that we want to get. In the next line, there will be N integers giving set S.

 
Output
For each test case, output one line containing exactly one integer, the minimal total cost. Take a look at the sample output for format.

 
Sample Input
2
3 2
1 2 4
4 2
4 7 10 1
 
Sample Output
Case 1: 1
Case 2: 18

Hint

The answer will fit into a 32-bit signed integer.

 
Source
 
Recommend
zhengfeng

题意:

给出一组数,把这组数分成m个集合,使得每个集合的(MAX-MIN)^2的和最小。

题解:

1.首先可以确定:每个集合的数值跨度应该尽量小,所以可以先对这些数进行排序,被分成一组的数必定是相连的。

2.设dp[i][j]为:第j个数属于第i个集合时的最小值,那么:dp[i][j] = min(dp[i-1][k] + (val[i] - val[k+1)^2),其中 i-1<=k<=j-1。

3.根据上述的状态转移方程,可算得时间复杂度为O(n^3),无法接受。因此可以用斜率优化。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e4+; int val[MAXN], dp[MAXN][MAXN];
int q[MAXN], head, tail; int getUP(int i, int k1, int k2)
{
return (dp[i-][k1] + val[k1+]*val[k1+])-
(dp[i-][k2] + val[k2+]*val[k2+]);
} int getDOWN(int k1, int k2)
{
return *(val[k1+]-val[k2+]);
} int getDP(int i, int j, int k)
{
return dp[i-][k] + (val[j]-val[k+])*(val[j]-val[k+]);
} int main()
{
int n, m, T;
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d%d", &n,&m);
for(int i = ; i<=n; i++)
scanf("%d", &val[i]); sort(val+, val++n);
for(int i = ; i<=n; i++) //初始化第一段
dp[][i] = (val[i]-val[])*(val[i]-val[]);
for(int i = ; i<=m; i++) //从i-1段转移到i段
{
head = tail = ;
q[tail++] = i-; //i-1段最少要有i-1个数,故从i-1开始
for(int j = i; j<=n; j++) //i段最少要有i个数,故从i开始
{
while(head+<tail && getUP(i,q[head+],q[head])<getDOWN(q[head+], q[head])*val[j]) head++;
dp[i][j] = getDP(i,j,q[head]); while(head+<tail && getUP(i,j,q[tail-])*getDOWN(q[tail-],q[tail-])<=
getUP(i,q[tail-],q[tail-])*getDOWN(j,q[tail-])) tail--;
q[tail++] = j;
}
}
printf("Case %d: %d\n", kase, dp[m][n]);
}
}

HDU3480 Division —— 斜率优化DP的更多相关文章

  1. hdu 3480 Division(斜率优化DP)

    题目链接:hdu 3480 Division 题意: 给你一个有n个数的集合S,现在让你选出m个子集合,使这m个子集合并起来为S,并且每个集合的(max-min)2 之和要最小. 题解: 运用贪心的思 ...

  2. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

  3. HDU2829 Lawrence —— 斜率优化DP

    题目链接:https://vjudge.net/problem/HDU-2829 Lawrence Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  4. bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)

    题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地 ...

  5. bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)

    题目链接: 1096: [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L ...

  6. [BZOJ3156]防御准备(斜率优化DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3156 分析: 简单的斜率优化DP

  7. 【BZOJ-1096】仓库建设 斜率优化DP

    1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3719  Solved: 1633[Submit][Stat ...

  8. BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP

    1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...

  9. BZOJ 3156: 防御准备 斜率优化DP

    3156: 防御准备 Description   Input 第一行为一个整数N表示战线的总长度. 第二行N个整数,第i个整数表示在位置i放置守卫塔的花费Ai. Output 共一个整数,表示最小的战 ...

随机推荐

  1. 2017-11-07-noip模拟题

    T1 数学老师的报复 矩阵快速幂模板,类似于菲波那切数列的矩阵 [1,1]*[A,1 B,0] #include <cstdio> #define LL long long inline ...

  2. git使用笔记一:

    Get code into Bitbucket fast using the command line Set up your local directory Set up Git on your m ...

  3. poj 2104 K-th Number(主席树

    Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 59058   Accepted: 20529 Case Time Limi ...

  4. javascript --- 对象之间的继承

    了解这一章之前,先把我们之前讲到的以构造函数创建对象为前提的继承抛到一边. 首先,我们先用一个var o = {}创建一个没有任何属性的空对象作为我们的‘画板’,然互在逐步向这个画板里添加属性,和方法 ...

  5. GCD CoreData 简化CoreData操作(转)

    来自会员带睡帽的青蛙的分享: 短话长说,开始写这个小工具到现在有两个月了,虽然东西少,但是很精练,改了又改,期间有不少问题 在坛子里获得了不少帮助 谢谢各位大大. 就是两个文件一个类 CoreData ...

  6. 邁向IT專家成功之路的三十則鐵律 鐵律十五:IT人生活之道-赤子之心

    人的年齡與身體可以因歲月的無情不斷老化,但我們的這一顆心可千萬不要跟著老化.身為IT工作者的我們,每天除了要面對那死板僵硬的電腦挑戰之外,可能還要面臨許多人事方面的紛擾.這時候如果在平日的生活之中,仍 ...

  7. Mac -- 安装及使用Docker

    安装这三个软件. 有两个安装包:  和  安装完使用挺简的. 更多内容官网查看: https://docs.docker.com/

  8. Linux如何显示文件指定行数的内容;显示第一行、中间几行和最后几行

    1.tail -n +/-数字 文件名 2.head -n 数字 文件名 3.sed -n "开始行,结束行p" 文件名 4.sed -n '1p;20,40p; "显示 ...

  9. 【Sprint3冲刺之前】日历表的事件处理和管理(刘铸辉)

    我的Sprint2冲刺——日历表的事件处理和管理(刘铸辉,刘静) 我的Sprint2冲刺计划领到的任务是和静姐结对编程,完成日历表的事件处理和管理,下面详细讲解下技术细节. 1.设计结构图 首先要画出 ...

  10. javascript 高级编程系列 - 函数

    一.函数创建 1. 函数声明 (出现在全局作用域,或局部作用域) function add (a, b) { return a + b; } function add(a, b) { return a ...