题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3480

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 999999/400000 K (Java/Others)

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

题意:

给出含有N元素的集合S,选取M个S的子集,要求满足SU S2 U … U SM = S;

定义一个集合的最大元素为MAX,最小元素为MIN,它的花费为(MAX - MIN)2,现要求所有子集的总花费最少为多少。

题解:

先将S内元素从小到大排列,然后将这N个元素的序列分成M组(因为若有重叠元素,必然会使得花费增加);

那么假设dp[i][j]为前i个数分成j组的最小花费,那么求出dp[N][M]即可回答问题;

状态转移方程为dp[i][j] = min{ dp[k][j-1] + (S[i] - S[k+1])2 },j-1≤k<i;

那么当j固定时,计算dp[i][j]时需要枚举k,若k可能取值到a,b两点,且j-1≤a<b<i,

若有 dp[b][j-1] + (S[i] - S[b+1])2 ≤ dp[a][j-1] + (S[i] - S[a+1])2,则b点优于a点;

将上式变形,得到:

b点优于a点 <=> 

再然后就是斜率优化的老套路了(斜率优化的详情查看斜率DP分类里之前的文章),就不再赘述。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=+; int n,m,S[maxn];
int dp[maxn][maxn];
int q[maxn],head,tail; int up(int a,int b,int j) //g(a,b)的分子部分
{
return (dp[b][j-]+S[b+]*S[b+])-(dp[a][j-]+S[a+]*S[a+]);
}
int down(int a,int b) //g(a,b)的分母部分
{
return *S[b+]-*S[a+];
} int main()
{
int t;
scanf("%d",&t);
for(int kase=;kase<=t;kase++)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&S[i]);
sort(S+,S+n+); for(int i=;i<=n;i++) dp[i][]=(S[i]-S[])*(S[i]-S[]);
for(int j=;j<=m;j++)
{
head=tail=;
q[tail++]=j-;
for(int i=j;i<=n;i++)
{
while(head+<tail)
{
int a=q[head], b=q[head+];
if(up(a,b,j)<=S[i]*down(a,b)) head++; //g(a,b)<=S[i]
else break;
}
int k=q[head];
dp[i][j]=dp[k][j-]+(S[i]-S[k+])*(S[i]-S[k+]); while(head+<tail)
{
int a=q[tail-], b=q[tail-];
if(up(a,b,j)*down(b,i)>=up(b,i,j)*down(a,b)) tail--; //g(a,b)>=g(b,i)
else break;
}
q[tail++]=i;
}
} printf("Case %d: %d\n",kase,dp[n][m]);
}
}

注意DP边界的初始化。

HDU 3480 - Division - [斜率DP]的更多相关文章

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

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

  2. HDU 2829 - Lawrence - [斜率DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 T. E. Lawrence was a controversial figure during ...

  3. ACM-ICPC 2016 沈阳赛区现场赛 I. The Elder && HDU 5956(斜率DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5956 题意:一颗树上每条边有个权值,每个节点都有新闻要送到根节点就是1节点,运送过程中如果不换青蛙就是 ...

  4. HDU 3480 Division(斜率DP裸题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3480 题目大意:将n个数字分成m段,每段价值为(该段最大值-该段最小值)^2,求最小的总价值. 解题思 ...

  5. HDU 3480 Division(斜率优化+二维DP)

    Division Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 999999/400000 K (Java/Others) Tota ...

  6. HDU 3480 Division DP斜率优化

    解题思路 第一步显然是将原数组排序嘛--然后分成一些不相交的子集,这样显然最小.重点是怎么分. 首先,我们写出一个最暴力的\(DP\): 我们令$F[ i ][ j ] $ 为到第\(i\)位,分成\ ...

  7. HDU 3480 division

    题目大意:一个有n个数的集合,现在要求将他分成m+1个子集,对子集i设si表示该集合中最大数与最小数的差的平方.求所有si的和的最小值.n<=10000,m<=5000. 分析:最优解的m ...

  8. hdu 3480 Division(四边形不等式优化)

    Problem Description Little D is really interested in the theorem of sets recently. There’s a problem ...

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

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

随机推荐

  1. RedHat6.5-Linux安装telnet服务

    1 下载以下三个包 telnet-0.17-47.el6.x86_64.rpm(telnet客户端) telnet-server-0.17-47.el6.x86_64.rpm(telnet服务端) x ...

  2. virtio-netdev 数据包的发送

    在前面几文中已经大体介绍了virtio的重要组成,包含virtio net设备的创建,vring的创建,与virtio设备的交互方式,我们就从网络数据包的发送角度来看下virtio的详细使用流程. [ ...

  3. 解决win10休眠后无法唤醒

    在控制面板-电源选项-编辑计划设置-高级电源设置中把"睡眠"的选项中休眠调整为从不,"电源按键和盖子"选项中也都设为睡眠,这样使得无论你是使用电池还是电源,系统 ...

  4. ios开发周期之--(向上,向下,四舍五入)取整

    ceil(x)返回不小于x的最小整数值(然后转换为double型). floor(x)返回不大于x的最大整数值. round(x)返回x的四舍五入整数值.

  5. Springboot启动后报错【This application has no explicit mapping for /error, so you are seeing this as a fallback····】

    This application has no explicit mapping for /error, so you are seeing this as a fallback. Wed Dec 1 ...

  6. 【数据分析】Superset 之四 直接安装

    apt install python-pip pip install --upgrade pip apt-get install build-essential libssl-dev libffi-d ...

  7. 【代码审计】CLTPHP_v5.5.3后台任意文件下载漏洞分析

      0x00 环境准备 CLTPHP官网:http://www.cltphp.com 网站源码版本:CLTPHP内容管理系统5.5.3版本 程序源码下载:https://gitee.com/chich ...

  8. Bypass 护卫神SQL注入防御(多姿势)

    0x00 前言 ​ 护卫神一直专注服务器安全领域, 其中有一款产品,护卫神·入侵防护系统 ,提供了一些网站安全防护的功能,在IIS加固模块中有一个SQL防注入功能. 这边主要分享一下几种思路,Bypa ...

  9. Kafka 0.11版本新功能介绍 —— 空消费组延时rebalance

    在0.11之前的版本中,多个consumer实例加入到一个空消费组将导致多次的rebalance,这是由于每个consumer instance启动的时间不可控,很有可能超出coordinator确定 ...

  10. Android 自定义 View 浅析

    Android 自定义 View 浅析 概括 说到自定义 View ,就一定得说说 android 系统的UI绘制流程.再说这个流程之前,我们先看一下在每一个 activity 页面中我们的布局 ui ...