斜率优化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
  1. 2
  2. 3 2
  3. 1 2 4
  4. 4 2
  5. 4 7 10 1
 
Sample Output
  1. Case 1: 1
  2. Case 2: 18
  3. Hint
  4. The answer will fit into a 32-bit signed integer.
  5.  
 
Source
 

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. const int maxn=11000;
  9.  
  10. int n,m;
  11. int dp[maxn/2][maxn],a[maxn];
  12. int q[maxn],head,tail;
  13.  
  14. int main()
  15. {
  16. int T_T,cas=1;
  17. scanf("%d",&T_T);
  18. while(T_T--)
  19. {
  20. scanf("%d%d",&n,&m);
  21. for(int i=1;i<=n;i++)
  22. scanf("%d",a+i);
  23. sort(a+1,a+n+1);
  24. for(int i=1;i<=n;i++)
  25. dp[1][i]=(a[i]-a[1])*(a[i]-a[1]);
  26. for(int i=2;i<=m;i++)
  27. {
  28. head=tail=0;
  29. q[tail++]=i-1;
  30. for(int j=i;j<=n;j++)
  31. {
  32. while(head+1<tail)
  33. {
  34. int p1=q[head];
  35. int p2=q[head+1];
  36. int x1=a[p1+1],x2=a[p2+1];
  37. int y1=dp[i-1][p1]+x1*x1;
  38. int y2=dp[i-1][p2]+x2*x2;
  39. if((y2-y1)<=(x2-x1)*2*a[j]) head++;
  40. else break;
  41. }
  42. int k=q[head];
  43. dp[i][j]=dp[i-1][k]+(a[k+1]-a[j])*(a[k+1]-a[j]);
  44. while(head+1<tail)
  45. {
  46. int p1=q[tail-2],p2=q[tail-1],p3=j;
  47. int x1=a[p1+1],x2=a[p2+1],x3=a[p3+1];
  48. int y1=dp[i-1][p1]+x1*x1;
  49. int y2=dp[i-1][p2]+x2*x2;
  50. int y3=dp[i-1][p3]+x3*x3;
  51. if((y3-y2)*(x2-x1)<=(y2-y1)*(x3-x2)) tail--;
  52. else break;
  53. }
  54. q[tail++]=j;
  55. }
  56. }
  57. printf("Case %d: %d\n",cas++,dp[m][n]);
  58. }
  59. return 0;
  60. }

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. JS学习笔记-OO疑问之对象创建

    问一.引入工厂,解决反复代码 前面已经提到,JS中创建对象的方法,不难发现,主要的创建方法中,创建一个对象还算简单,假设创建多个类似的对象的话就会产生大量反复的代码. 解决:工厂模式方法(加入一个专门 ...

  2. DLNA它 Error, can&#39;t findlibavformat ! 解

    DLNA库版本号为libdlna-0.2.4 运行./configure出错: ------------------------------ Error, can't findlibavformat ...

  3. 【2014 Multi-University Training Contest 3 1002】/【HDU 4888】 Redraw Beautiful Drawings

    不easy啊.最终能够补第二个题了.! 顺便说一句:模版写残了就不要怪出题人啊 ~ (这残废模版研究了好长时间才找出错) 题目大意: 有一个n*m的矩阵.每个格子里都将有一个数.给你每一行数字之和和每 ...

  4. 在SQLAlter在现场一定的价值

    update AA set aa = replace(aa,'1234','规范') where aa like '%1234%'

  5. oracle 锁表、解锁的语句

     --1.以下的语句用来查询哪些对象被锁: select object_name,machine,s.sid,s.serial# from v$locked_object l,dba_object ...

  6. Windows 8 应用开发 - 磁贴

    原文:Windows 8 应用开发 - 磁贴      我们开发的应用在Win8 界面中会以磁贴形式展现,默认情况下磁贴会显示应用图标,即项目工程中的Logo.png图片文件.开发人员可按应用的需要使 ...

  7. Swing程序最佳架构设计—以业务对象为中心的MVC模式(转)

    前言: 我打算写一系列关于Swing程序开发的文章.这是由于最近我在做一个Swing产品的开发.长期做JavaEE程序,让我有些麻木了.Swing是设计模式的典范,是一件优雅的艺术品,是一件超越时代的 ...

  8. kindeditor-网页文字编辑

    实例下载地址:http://download.csdn.net/download/l294333475/7941759 <!DOCTYPE html PUBLIC "-//W3C//D ...

  9. 将DataTable 数据插入 SQL SERVER 数据库

    原文:将DataTable 数据插入 SQL SERVER 数据库 以下提供3中方式将DataTable中的数据插入到SQL SERVER 数据库: 一:使用sqlcommand.executenon ...

  10. hdu 2899 hdu 3400 三分/几何

    hdu2899 : 水提,直接三分,事实上求导后二分也能够. #include<iostream> #include<cstdio> using namespace std; ...