Division

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

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
 

【思路】

斜率优化+分配式DP。

设f[i][j]表示将前i个分作j个集合所得最小消费,则有转移方程式:

f[i][j]=min{ f[k][j-1]+(A[k]-A[j+1])^2 }

若有k>l,且决策k优于决策l则有:

f[k][j-1]-f[l][j-1]+sq(A[k+1])-sq(A[l+1]) <= 2*(A[k+1]-A[l+1])*A[i]

先进行j循环枚举f[][j],每一层维护一个单调队列即可。

乘除耗费时间悬殊,如果直接除这个题就超时了。

【代码】

 #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std; typedef double Do;
const int N = 1e4+;
const int M = +; int f[N][M],A[N],q[N];
int n,m,L,R;
int sq(int x) { return x*x; }
int UP(int l,int k,int j) {
return f[k][j-]-f[l][j-]+sq(A[k+])-sq(A[l+]);
}
int DN(int l,int k,int j) {
return *(A[k+]-A[l+]);
}
void read(int& x) {
char c=getchar(); while(!isdigit(c)) c=getchar();
x=; while(isdigit(c)) x=x*+c-'' , c=getchar();
}
int main() {
int T,kase=;
read(T);
while(T--) {
read(n),read(m);
for(int i=;i<=n;i++) read(A[i]);
sort(A+,A+n+);
for(int i=;i<=n;i++) f[i][]=sq(A[i]-A[]); //初始化第一层
for(int j=;j<=m;j++) {
L=R=;
for(int i=;i<=n;i++) {
while(L<R && UP(q[L],q[L+],j)<=A[i]*DN(q[L],q[L+],j)) L++;
int t=q[L];
f[i][j]=f[t][j-]+sq(A[i]-A[t+]);
while(L<R && UP(q[R-],q[R],j)*DN(q[R],i,j)>=UP(q[R],i,j)*DN(q[R-],q[R],j)) R--;
q[++R]=i;
}
}
printf("Case %d: %d\n",++kase,f[n][m]);
}
return ;
}

HDU 3480 Division(斜率优化+二维DP)的更多相关文章

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

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

  2. HDU 3480 - Division - [斜率DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3480 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  3. (hdu)5234 Happy birthday 二维dp+01背包

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5234 Problem Description Today is Gorwin’s birt ...

  4. HDU 4901 The Romantic Hero(二维dp)

    题目大意:给你n个数字,然后分成两份,前边的一份里面的元素进行异或,后面的一份里面的元素进行与.分的时候依照给的先后数序取数,后面的里面的全部的元素的下标一定比前面的大.问你有多上种放元素的方法能够使 ...

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

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

  6. HDU - 2159 FATE(二维dp之01背包问题)

    题目: ​ 思路: 二维dp,完全背包,状态转移方程dp[i][z] = max(dp[i][z], dp[i-1][z-a[j]]+b[j]),dp[i][z]表示在杀i个怪,消耗z个容忍度的情况下 ...

  7. 洛谷p1732 活蹦乱跳的香穗子 二维DP

    今天不BB了,直接帖原题吧  地址>>https://www.luogu.org/problem/show?pid=1732<< 题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现 ...

  8. 传纸条 NOIP2008 洛谷1006 二维dp

    二维dp 扯淡 一道比较基本的入门难度的二维dp,类似于那道方格取数,不过走过一次的点下次不能再走(看提交记录里面好像走过一次的加一次a[i][j]的也AC了,,),我记得当年那道方格取数死活听不懂, ...

  9. 洛谷P1048 采药 二维dp化一维

    题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个 ...

随机推荐

  1. sql - 选出指定范围的行

    Select no=Identity(int,1,1),* Into #temptable From dbo.tName order by fName --利用Identity函数生成记录序号 Sel ...

  2. union与union all 的区别

    Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并 ...

  3. 动态库DLL加载方式-静态加载和动态加载

    静态加载: 如果你有a.dll和a.lib,两个文件都有的话可以用静态加载的方式: message函数的声明你应该知道吧,把它的声明和下面的语句写到一个头文件中 #pragma comment(lib ...

  4. Struts2中EL表达式取值

    http://blog.csdn.net/cuihaiyang/article/details/41950141 (写的不错,可以知道为什么struts2可以用El取属性值的问题.正常el从reque ...

  5. 推送消息实现icon角标的动态显示

    在你自己服务器上做计数,客户端做减法并反馈给你的服务器 ,然后你服务器将需要显示的数字发送给苹果推送服务器(就是消息中的badge)比如:1,你服务器上发送出去3个推送消息到A手机           ...

  6. java_设计模式_组合模式_Composite Pattern(2016-08-12)

    概念: 组合模式(Composite Pattern)将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性. 有时候又叫做部分-整体模式,它使我们树 ...

  7. Linux下追踪函数调用,打印栈帧

    事情的起因是这样的,之前同事的代码有一个内存池出现了没有回收的情况.也就是是Pop出来的对象没有Push回去,情况很难复现,所以在Pop里的打印日志,跟踪是谁调用了它,我想在GDB调试里可以追踪调用的 ...

  8. StrongReference

    原创作品:未经本人允许,不得转载前段时间写项目时遇到了一个问题,就是从网络获取图片资源的问题,总是出现OOM异常,经过几天的努力,终于处理的还算是可以使用,OOM的处理一直都是很头疼的问题.对于三级缓 ...

  9. C#Winform开发平台企业版V4.0功能表

    企业版V4.0 - 功能列表及模板窗体 C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framew ...

  10. 利用java实现一个简单的远程监控程序

    一般的远程监控软件都是用c或者c++等语言开发的,而使用java如何来实现相同的功能呢. 首先我们先介绍一下一个简单的远程监控程序的实现原理. 功能一,远程屏幕监视 (1) 必须要有监控端与被监控端, ...