Cross the Wall

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)
Total Submission(s): 4479    Accepted Submission(s): 812

Problem Description
“Across the Great Wall, we can reach every corner in the world!” Now the citizens of Rectland want to cross the Great Wall. 
The Great Wall is a huge wall with infinite width and height, so the only way to cross is to dig holes in it. All people in Rectland can be considered as rectangles with varying width and height, and they can only dig rectangle holes in the wall. A person can pass through a hole, if and only if the person’s width and height is no more than the hole’s width and height both. To dig a hole with width W and height H, the people should pay W * H dollars. Please note that it is only permitted to dig at most K holes for security consideration, and different holes cannot overlap each other in the Great Wall. Remember when they pass through the wall, they must have their feet landed on the ground.
Given all the persons’ width and height, you are requested to find out the minimum cost for digging holes to make all the persons pass through the wall.
 
Input
There are several test cases. The first line of each case contains two numbers, N (1 <= N <= 50000) and K (1 <= K <= 100), indicating the number of people and the maximum holes allowed to dig. Then N lines followed, each contains two integers wi and hi (1 <= wi, hi <= 1000000), indicating the width and height of each person.
 
Output
Output one line for each test case, indicates the minimum cost.

 
Sample Input
2 1 1 100 100 1 2 2 1 100 100 1
 
Sample Output
10000 200
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3662 3661 3664 3665 3666 
题目描述:
给你n个人,每个人的宽度和高度已知,最多可以修k个门,修门的花费为门的面积,求最少花费。
d[i][j]=min(d[k][j-1]+p[k+1].w*p[i].h);时间复杂度为5*10^12,可以采用斜率优化或者四边形优化。
采取斜率优化:
设k1>k2,k1优于k2,dp[k1][j-1]+p[k1+1].w*p[i].h<=dp[k2][j-1]+p[k2+1].w*p[i].h;
化简得:dp[k1][j-1]-dp[k2][j-1]<p[i].h*(p[k2+1].w-p[k1+1].w)
预处理d[i][0]=inf,d[0][i]=0;
还需注意i和j的枚举顺序,先枚举j,再枚举i,每次枚举j的时候都需要重新初始化i的解集
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #define maxn 50100
  7. #define LL long long
  8. #define inf 0x3f3f3f3f3f3f3f3f3f3f
  9. using namespace std;
  10. LL dp[maxn][];
  11. int que[maxn];
  12. int n,k;
  13. int head,tail;
  14. struct node
  15. {
  16. LL w;
  17. LL h;
  18. };
  19. node p[maxn];
  20. bool cmp(node a,node b)
  21. {
  22. if(a.w==b.w)
  23. return a.h<b.h;
  24. else
  25. return a.w>b.w;
  26. }
  27. LL getdp(int i,int j,int k)
  28. {
  29. //return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);
  30. return dp[k][j-]+p[k+].w*p[i].h;
  31. }
  32.  
  33. LL getup(int j,int k1,int k2) //yj-yk部分 k1>k2
  34. {
  35. //return dp[j]+sum[j]*sum[j]-(dp[k]+sum[k]*sum[k]);
  36. return dp[k1][j-]-dp[k2][j-];
  37. }
  38. LL getdown(int j,int k1,int k2)
  39. {
  40. //return 2*(sum[j]-sum[k]);
  41. return p[k2+].w-p[k1+].w;
  42. }
  43.  
  44. void solve()
  45. {
  46. head=;
  47. tail=;
  48. que[tail++]=;
  49. dp[][]=;
  50. for(int i=;i<=n;i++)
  51. {
  52. dp[i][]=inf;
  53. dp[][i]=;
  54. }
  55. for(int j=;j<=k;j++)
  56. {
  57. head=tail=;
  58. que[tail++]=;
  59. for(int i=;i<=n;i++)
  60. {
  61. //从头开始找当前状态的最优决策,g[que[head+1],que[head]] < sum[i],说明que[head+1]比que[head]更优,删除que[head]
  62. while(head+ < tail && getup(j,que[head+],que[head]) <= getdown(j,que[head+],que[head]) * p[i].h )
  63. head++; //注意写成相乘,不然要考虑除数是否为负数
  64. dp[i][j]=getdp(i,j,que[head]);
  65.  
  66. //从尾往前,加入当前状态,如果g[i,que[tail]] < g[que[tail],que[tail-1]] ,可以排除que[tail]
  67. while(head+<tail && getup(j,i,que[tail-])*getdown(j,que[tail-],que[tail-])<=getup(j,que[tail-],que[tail-])*getdown(j,i,que[tail-]))
  68. tail--;
  69. que[tail++]=i;
  70. }
  71. }
  72.  
  73. /*for(int j=1;j<=k;j++)
  74. {
  75. for(int i=1;i<=n;i++)
  76. printf("%lld ",dp[i][j]);
  77. printf("\n");
  78. }*/
  79. printf("%lld\n",dp[n][k]);
  80.  
  81. }
  82. int main()
  83. {
  84. while(~scanf("%d%d",&n,&k))
  85. {
  86. //init();
  87. for(int i=;i<=n;i++)
  88. scanf("%lld%lld",&p[i].w,&p[i].h);
  89. sort(p+,p+n+,cmp);
  90.  
  91. int j=;
  92. for(int i=;i<=n;i++)
  93. {
  94. if(p[i].h<p[j].h)
  95. continue;
  96. else
  97. {
  98. p[++j]=p[i];
  99. }
  100. }
  101. n=j;
  102. //for(int i=1;i<=n;i++)
  103. // printf("%lld %lld\n",p[i].w,p[i].h);
  104. solve();
  105. }
  106. return ;
  107. }

四边形优化:

目前位置接触到两种形式的方程可以采用四边形优化:

1

a d[i][j]=min(d[i-1][k]+p[k+1].w*p[i].h)

写成这种形式,而不是上面那种,是因为四边形优化的s[i][j]的递推顺序好写

b s[i-1][j]<s[i][j]<s[i][j+1];

观察a和b式,i从小到大,j从大到小.然后初始化第一行和第n+1列,进行递推。

2 (类似于石子合并)

d[i][j]=d[i][k]+d[k+1][j]+w[i][j]

s[i][j-1]<s[i][j]<s[i+1][j]

这时候的递推顺序,是通过先枚举长度,再枚举起点,然后就可以现在要算的状态之前都算过了。

(超时代码,目前只能写到这了)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #define maxn 50100
  7. #define LL long long
  8. #define inf 0x3f3f3f3f3f3f3f3f3f3f
  9. using namespace std;
  10. LL d[maxn][];
  11. int w[maxn][];
  12. int n,k;
  13. struct node
  14. {
  15. LL w;
  16. LL h;
  17. };
  18. node p[maxn];
  19. bool cmp(node a,node b)
  20. {
  21. if(a.w==b.w)
  22. return a.h<b.h;
  23. else
  24. return a.w>b.w;
  25. }
  26. void solve()
  27. {
  28. for(int i=;i<=n;i++)
  29. {
  30. d[][i]=p[].w*p[i].h;
  31. w[][i]=;
  32.  
  33. }
  34.  
  35. for(int i=;i<=k;i++)
  36. {
  37. w[i][n+]=n;
  38. for(int j=n;j>=i;j--)
  39. {
  40. d[i][j]=inf;
  41. for(int s=w[i-][j];s<=w[i][j+];s++)
  42. {
  43. if(d[i][j]>(d[i-][s]+p[s+].w*p[j].h) )
  44. {
  45. d[i][j]=(d[i-][s]+p[s+].w*p[j].h);
  46. w[i][j]=s;
  47. }
  48. }
  49. }
  50. }
  51. /*for(int i=0;i<=k;i++)
  52. {
  53. for(int j=0;j<=n+1;j++)
  54. printf("%d ",w[i][j]);
  55. printf("\n");
  56. }
  57. printf("\n");
  58. for(int i=0;i<=k;i++)
  59. {
  60. for(int j=0;j<=n;j++)
  61. printf("%lld ",d[i][j]);
  62. printf("\n");
  63. }*/
  64. printf("%lld\n",d[k][n]);
  65.  
  66. }
  67. int main()
  68. {
  69. while(~scanf("%d%d",&n,&k))
  70. {
  71. //init();
  72. memset(d,,sizeof(d));
  73. for(int i=;i<=n;i++)
  74. scanf("%lld%lld",&p[i].w,&p[i].h);
  75. sort(p+,p+n+,cmp);
  76.  
  77. int j=;
  78. for(int i=;i<=n;i++)
  79. {
  80. if(p[i].h<p[j].h)
  81. continue;
  82. else
  83. {
  84. p[++j]=p[i];
  85. }
  86. }
  87. n=j;
  88.  
  89. solve();
  90. }
  91. return ;
  92. }

hdu 3669(斜率优化DP)的更多相关文章

  1. HDU 4258 斜率优化dp

    Covered Walkway Time Limit: 30000/10000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  2. HDU 2829 斜率优化DP Lawrence

    题意:n个数之间放m个障碍,分隔成m+1段.对于每段两两数相乘再求和,然后把这m+1个值加起来,让这个值最小. 设: d(i, j)表示前i个数之间放j个炸弹能得到的最小值 sum(i)为前缀和,co ...

  3. hdu 3045 斜率优化DP

    思路:dp[i]=dp[j]+sum[i]-sum[j]-(i-j)*num[j+1]; 然后就是比较斜率. 注意的时这里j+t<=i: #include<iostream> #in ...

  4. Print Article HDU - 3507 -斜率优化DP

    思路 : 1,用一个单调队列来维护解集. 2,假设队列中从头到尾已经有元素a b c.那么当d要入队的时候,我们维护队列的下凸性质, 即如果g[d,c]<g[c,b],那么就将c点删除.直到找到 ...

  5. HDU 3507 斜率优化dp

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  6. HDU 3507斜率优化dp

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  7. HDU 3507 斜率优化 DP Print Article

    在kuangbin巨巨博客上学的. #include <iostream> #include <cstdio> #include <cstring> #includ ...

  8. HDU 2993 MAX Average Problem(斜率优化DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给定一个长度为n(最长为10^5)的正整数序列,求出连续的最短为k的子序列平均值的最大 ...

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

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

随机推荐

  1. BZOJ1986: [USACO2004 Dec] Dividing the Path 划区灌溉

    L<=1000000的土地上用长度在2*A~2*B的线段覆盖所有点,且给定n<=1000个区间,每个区间上只允许有一条线段,求最少多少线段,无解-1. f[i]表示填前i个土地最少线段,f ...

  2. Maven 手动添加本地jar包

    mvn install:install-file -Dfile=jar绝对路径 -DgroupId=项目组织唯一的标识符 -DartifactId=项目的唯一的标识符 -Dversion=jar版本 ...

  3. POJ 2348 Euclid's Game【博弈】

    题目链接: http://poj.org/problem?id=2348 题意: 给定两个数,两个人每次从较大数中减去较小数的倍数,谁先得到0谁获胜,为谁赢? 分析: 令一种可能出现的整数对为(a,b ...

  4. 【纯净版windows系统】U盘启动制作图文教程

    无废话,按照步骤来就可以. 1.一个大于4G的U盘(格式化)准备好U盘,请注意制作过程中对U盘有格式化操作,有用的东西请先备份 2.UltraISO(软碟通软件)下载安装百度“软碟通”,或者访问 ht ...

  5. O2O助汪峰成功逆袭,汪峰最终上头条了

    8月2日七夕情人节,汪峰<峰暴来临>演唱会在鸟巢10万人体育场唱响,各大报纸.站点娱乐板块并没有等来汪峰向国际章求婚的"头条",只是,与乐视合作现场演出+付费直播的O2 ...

  6. leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  7. 基于Spring-SpringMVC-Mybatis的简单样例

    复习下 好久没搞过撸过代码了! 这个样例包括一个完整的增删改查! 源代码地址http://download.csdn.net/detail/wangdianyong/8909903

  8. Vue调试工具 vue-devtools

    vue-devtools是一款基于chrome浏览器的插件,可以帮我们快速调试vue项目 vue-devtools手动安装: 第一步:找到vue-devtools的github项目(https://g ...

  9. random模块的使用

    random模块用于生成随机数 import random print random.random() #用于生成小于1大于0的数 print random.randint(1,5) #生成大于等于1 ...

  10. 【JS】JavaScript引擎的内部执行机制

     近期在复习JavaScript,看到setTimeout函数时.想起曾经刚学时,在一本书上看过setTimeout()里的回调函数执行的间隔时间有昌不是后面设置的值.曾经没想太多.网上看了JS大 ...