斜率优化DP。

。。。

对数组排序后。dp【i】【j】表示对前j个物品分i段的最少代价,dp【i】【j】= min{ dp【i-1】【k】+(a【k+1】-a【j】)^2 }复杂度m*n^2      斜率优化一下就能够了。

Division

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

Total Submission(s): 3008    Accepted Submission(s): 1173

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
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn=11000; int n,m;
int dp[maxn/2][maxn],a[maxn];
int q[maxn],head,tail; int main()
{
int T_T,cas=1;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
dp[1][i]=(a[i]-a[1])*(a[i]-a[1]);
for(int i=2;i<=m;i++)
{
head=tail=0;
q[tail++]=i-1;
for(int j=i;j<=n;j++)
{
while(head+1<tail)
{
int p1=q[head];
int p2=q[head+1];
int x1=a[p1+1],x2=a[p2+1];
int y1=dp[i-1][p1]+x1*x1;
int y2=dp[i-1][p2]+x2*x2;
if((y2-y1)<=(x2-x1)*2*a[j]) head++;
else break;
}
int k=q[head];
dp[i][j]=dp[i-1][k]+(a[k+1]-a[j])*(a[k+1]-a[j]);
while(head+1<tail)
{
int p1=q[tail-2],p2=q[tail-1],p3=j;
int x1=a[p1+1],x2=a[p2+1],x3=a[p3+1];
int y1=dp[i-1][p1]+x1*x1;
int y2=dp[i-1][p2]+x2*x2;
int y3=dp[i-1][p3]+x3*x3;
if((y3-y2)*(x2-x1)<=(y2-y1)*(x3-x2)) tail--;
else break;
}
q[tail++]=j;
}
}
printf("Case %d: %d\n",cas++,dp[m][n]);
}
return 0;
}

HDOJ 3480 Division的更多相关文章

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

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

  2. 【HDOJ】3480 Division

    斜率dp+滚动数组. /* 3480 */ #include <iostream> #include <sstream> #include <string> #in ...

  3. 【HDU】3480 Division

    http://acm.hdu.edu.cn/showproblem.php?pid=3480 题意:一个n个元素的集合S要求分成m个子集且子集并为S,要求$\sum_{S_i} (MAX-MIN)^2 ...

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

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

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

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

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

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

  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 3480 Division DP斜率优化

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

随机推荐

  1. 《5》CentOS7.0+OpenStack+kvm云平台的部署—组态Horizon

    感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限,错误之处在所难免,欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  2. linux上安装Drupal

    linux上安装Drupal 前言:国内用drupal的并不太多,网上资料也很少.要注意的是drupal尽量别使用apt来安装,特别是ubuntu平台的drupal做出了一定的更改,会妨碍后期的学习和 ...

  3. HDOJ 4687 Boke and Tsukkomi 一般图最大匹配带花树+暴力

    一般图最大匹配带花树+暴力: 先算最大匹配 C1 在枚举每一条边,去掉和这条边两个端点有关的边.....再跑Edmonds得到匹配C2 假设C2+2==C1则这条边再某个最大匹配中 Boke and ...

  4. 如何处理 Windows Phone 8 动态砖变成黑白砖

    原文:如何处理 Windows Phone 8 动态砖变成黑白砖 ? 问题的来龙去脉 我的 Windows Phone 8 动态砖变成黑白砖,所有图示和文字变成黑白,该如何处理? ? 问题的发生原因 ...

  5. effective c++ 条款10 handle assignment to self operator =

    非强制性,但是个好习惯 当使用连锁赋值时很有用 x=y=z=10; class Window { public: Window& operator=(int size) { ... retur ...

  6. clientIDMode的应用

    以前我们可以通过ClientID在JavaScript脚本中服务器端控件.比如: document.getElementById("<%=控件.ClientID %>" ...

  7. Win7 IIS配置 applicationHost.config 错误:无法识别的特性“setProfileEnvironment” 解决方法

    Win7下配置IIS时容易出现这样的错误提示:这是百度知道上面另一个人提问的图,我的显示行号133 解决方法: 到C:\inetpub\history中找到最近一次的applicationHost.c ...

  8. 微软中国裁员曝光:在CD结束后!薪酬不变!

    聚众抗议的前诺基亚员工(腾讯科技配图) 腾讯科技 郭晓峰 腾讯科技刚刚获取了一份微软设备事业部中国区管理团队4日晚间发给被裁员工的补偿方案邮件. 邮件内容显示,微软承诺在收购诺基亚交易结束(2014年 ...

  9. Cocos2d-x场景变化相关功能介绍

    现场由导演级交换机Director实现.之间的相关的功能,如下面: runWithScene(Scene* scene).该函数能够执行场景.仅仅能在启动第一个场景时候调用该函数.假设已经有一个场景执 ...

  10. 多功能截图工具(WinSnap)4.5.6 绿色汉化版(附注册码)

    http://www.uzzf.com/Soft/9840.html 注册名:www.uzzf.com 注册码:FGE5ML-XD2C0G33-GCMDLRB5