Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)

Total Submission(s): 4619    Accepted Submission(s): 846


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

这题如果用普通的区间dp会超时,要用斜率优化。先使这些矩形按宽从大到小排序,如果宽相同就按高度从高到低排序,然后从第一个开始依次把矩形加入集合,如果当前访问的矩形的高度比最后加入集合的矩形的高度小,那么最后加入集合的矩形一定能够覆盖当前访问的,这样这个矩形就可以跳过不加入集合。注意,最后进行dp的矩形一定要满足宽度递增而高度递减。然后就可以进行dp,用dp[i][j]表示前i个数分成j组所用的最小面积,那么dp[i][j]=min(dp[k][j-1]+a[k+1].h*a[i].w)(j-1<=k<i).设k1<k2且k2比k1优,那么dp[k2][j-1]+w[i]*h[k2+1]<=dp[k1][j-1]+w[i]*h[k1+1],则(dp[k2][j-1]-dp[k1][j-1])/(h[k1+1]-h[k2+1)<=w[i],这里有个注意的地方,因为分母是含k1的项减去含k2的项,所以我们要转化一下,令x=-(h[k+1],那么

分母就变为x2-x1了,又因为随着i的递增,w[i]增加,所以如果满足这个斜率不等式,k2一定是比k1优的,所以可以删除k1.


#include<iostream>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 999999999999999999
#define maxn 50006
ll dp[maxn][106];
int s[maxn][106];
struct node{
ll w,h;
}a1[maxn],a[maxn]; bool cmp(node a,node b){
if(a.w==b.w)return a.h>b.h;
return a.w>b.w;
}
int q[111111],j; ll getup(int k){
return dp[k][j-1];
}
ll getdown(int k){
return -a[k+1].h;
} int main()
{
int n,m,i,tot,k,front,rear;
ll ans;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++){
scanf("%lld%lld",&a1[i].w,&a1[i].h);
}
sort(a1+1,a1+1+n,cmp);
tot=1;a[1].w=a1[1].w;a[1].h=a1[1].h;
for(i=2;i<=n;i++){
if(a1[i].h<=a[tot].h)continue;
tot++;
a[tot].w=a1[i].w;a[tot].h=a1[i].h;
}
reverse(a+1,a+1+tot);
for(i=1;i<=tot;i++){
dp[i][1]=a[i].w*a[1].h;
} for(j=2;j<=m;j++){
front=rear=0;
q[rear]=j-1;
for(i=j;i<=tot;i++){
while(front<rear && getup(q[front+1])-getup(q[front])<=a[i].w*(getdown(q[front+1])-getdown(q[front])) ){
front++;
}
k=q[front];
dp[i][j]=dp[k][j-1]+a[k+1].h*a[i].w;
while(front<rear && (getup(q[rear])-getup(q[rear-1]))*(getdown(i)-getdown(q[rear]))>=(getup(i)-getup(q[rear]) )*(getdown(q[rear])-getdown(q[rear-1])) ){
rear--;
}
rear++;
q[rear]=i;
}
}
ans=inf;
for(j=1;j<=m;j++){
ans=min(ans,dp[tot][j]);
}
printf("%lld\n",ans);
}
return 0;
}

hdu3669 Cross the Wall的更多相关文章

  1. hdu 3669 Cross the Wall(斜率优化DP)

    题目连接:hdu 3669 Cross the Wall 题意: 现在有一面无限大的墙,现在有n个人,每个人都能看成一个矩形,宽是w,高是h,现在这n个人要通过这面墙,现在只能让你挖k个洞,每个洞不能 ...

  2. HDU 3669 Cross the Wall

    题目大意 给定 \(N\) 个矩形的宽和高, \((h_1, w_1), (h_2, w_2), \dots, (h_n w_n)\) . 现需要确定 \(k\) (\(k \le K\), \(K\ ...

  3. HDU 3669 Cross the Wall(斜率DP+预处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3669 题目大意:有n(n<=50000)个矩形,每个矩形都有高和宽,你可以在墙上最多挖k个洞使得 ...

  4. UVALive 5097 Cross the Wall

    贪心思想,$dp$,斜率优化. 首先将人按照$w$从大到小排序,如果$w$一样,按$h$从大到小排.这样一来,某位置之后,比该位置$h$小的都是不需要考虑的. 因此,形成了如下图所示的结果: 即第一个 ...

  5. 动态规划DP的斜率优化 个人浅解 附HDU 3669 Cross the Wall

    首先要感谢叉姐的指导Orz 这一类问题的DP方程都有如下形式 dp[i] = w(i) + max/min(a(i)*b(j) + c(j)) ( 0 <= j < i ) 其中,b, c ...

  6. HDU 3669 [Cross the Wall] DP斜率优化

    问题分析 首先,如果一个人的\(w\)和\(h\)均小于另一个人,那么这个人显然可以被省略.如果我们将剩下的人按\(w[i]\)递增排序,那么\(h[i]\)就是递减. 之后我们考虑DP. 我们设\( ...

  7. [kuangbin带你飞]专题二十 斜率DP

            ID Origin Title   20 / 60 Problem A HDU 3507 Print Article   13 / 19 Problem B HDU 2829 Lawr ...

  8. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

  9. 【转】斜率优化DP和四边形不等式优化DP整理

    (自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...

随机推荐

  1. 【Linux】history用法

    通过history命令可以查看我们在系统中输入过的命令 history命令的一些常用参数 -c  清空内存中命令历史 -d #  删除指定的历史命令,比如 history -d 100 ,就是删除第1 ...

  2. 攻防世界 - Web(二)

    supersqli: (!!!) 1.判断有误注入,1'报错, 1' 报错,1'# 正常且为True,1' and 1=1# 正常且为True,1' and 1=2# 正常且为False,所以它里边存 ...

  3. 两节锂电池充电芯片,和保护IC的接法

    1.两节锂电池的充电电路:可以分为三种方式. 第一种,USB口的5V输入,使用一颗SOT23-6的升压IC,直接升压到8.4V.电流在1A以下.优点是成本最低,缺点是,没有锂电池充电控制逻辑,和锂电池 ...

  4. PWN_ret2text,ret2syscall,ret2shellcode

    首先了解下Linux中的保护机制(具体的绕过等后续再说) 1.canary(栈保护) 在函数开始时就随机产生一个值,将这个值CANARY放到栈上紧挨ebp的上一个位置,当攻击者想通过缓冲区溢出覆盖eb ...

  5. 深圳某小公司面试题:AQS是什么?公平锁和非公平锁?ReentrantLock?

    AQS总体来说没有想象中那么难,只要了解它的实现框架,那理解起来就不是什么问题了. AQS在Java还是占很重要的地位的,面试也是经常会问. 目前已经连载11篇啦!进度是一周更新两篇,欢迎持续关注 [ ...

  6. DNS是如何工作的?

    今天很多人都在讲域名系统和互联网作为一个整体是如何工作的,域名系统---也就是大家所熟知的DNS.不幸的是,对于天龙人和普通人来说,他们并不了解DNS到底是什么鬼.今天就来聊聊DNS,和那些想了解DN ...

  7. 详解Mybatisplus

    详解Mybatisplus ​ MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 特性: 无侵入**:只 ...

  8. 在QML 中用javascritpt 将中文转换拼音,可以在音标

    项目需要, 今天整理了一下.在QML调用javascrit将中文汉字转换成拼音. 感觉执行效率低.下面是主要代码. 具体代码请参考QMLPinyin 代码 ```import "./piny ...

  9. 大数据系列4:Yarn以及MapReduce 2

    系列文章: 大数据系列:一文初识Hdfs 大数据系列2:Hdfs的读写操作 大数据谢列3:Hdfs的HA实现 通过前文,我们对Hdfs的已经有了一定的了解,本文将继续之前的内容,介绍Yarn与Yarn ...

  10. 【汇编实践】go assembly

    https://mp.weixin.qq.com/s/B577CdUkWCp_XgUc1VVvSQ asmshare/layout.md at master · cch123/asmshare htt ...