hdu 3669(斜率优化DP)
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
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.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 50100
#define LL long long
#define inf 0x3f3f3f3f3f3f3f3f3f3f
using namespace std;
LL dp[maxn][];
int que[maxn];
int n,k;
int head,tail;
struct node
{
LL w;
LL h;
};
node p[maxn];
bool cmp(node a,node b)
{
if(a.w==b.w)
return a.h<b.h;
else
return a.w>b.w;
}
LL getdp(int i,int j,int k)
{
//return dp[j]+m+(sum[i]-sum[j])*(sum[i]-sum[j]);
return dp[k][j-]+p[k+].w*p[i].h;
} LL getup(int j,int k1,int k2) //yj-yk部分 k1>k2
{
//return dp[j]+sum[j]*sum[j]-(dp[k]+sum[k]*sum[k]);
return dp[k1][j-]-dp[k2][j-];
}
LL getdown(int j,int k1,int k2)
{
//return 2*(sum[j]-sum[k]);
return p[k2+].w-p[k1+].w;
} void solve()
{
head=;
tail=;
que[tail++]=;
dp[][]=;
for(int i=;i<=n;i++)
{
dp[i][]=inf;
dp[][i]=;
}
for(int j=;j<=k;j++)
{
head=tail=;
que[tail++]=;
for(int i=;i<=n;i++)
{
//从头开始找当前状态的最优决策,g[que[head+1],que[head]] < sum[i],说明que[head+1]比que[head]更优,删除que[head]
while(head+ < tail && getup(j,que[head+],que[head]) <= getdown(j,que[head+],que[head]) * p[i].h )
head++; //注意写成相乘,不然要考虑除数是否为负数
dp[i][j]=getdp(i,j,que[head]); //从尾往前,加入当前状态,如果g[i,que[tail]] < g[que[tail],que[tail-1]] ,可以排除que[tail]
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-]))
tail--;
que[tail++]=i;
}
} /*for(int j=1;j<=k;j++)
{
for(int i=1;i<=n;i++)
printf("%lld ",dp[i][j]);
printf("\n");
}*/
printf("%lld\n",dp[n][k]); }
int main()
{
while(~scanf("%d%d",&n,&k))
{
//init();
for(int i=;i<=n;i++)
scanf("%lld%lld",&p[i].w,&p[i].h);
sort(p+,p+n+,cmp); int j=;
for(int i=;i<=n;i++)
{
if(p[i].h<p[j].h)
continue;
else
{
p[++j]=p[i];
}
}
n=j;
//for(int i=1;i<=n;i++)
// printf("%lld %lld\n",p[i].w,p[i].h);
solve();
}
return ;
}
四边形优化:
目前位置接触到两种形式的方程可以采用四边形优化:
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]
这时候的递推顺序,是通过先枚举长度,再枚举起点,然后就可以现在要算的状态之前都算过了。
(超时代码,目前只能写到这了)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 50100
#define LL long long
#define inf 0x3f3f3f3f3f3f3f3f3f3f
using namespace std;
LL d[maxn][];
int w[maxn][];
int n,k;
struct node
{
LL w;
LL h;
};
node p[maxn];
bool cmp(node a,node b)
{
if(a.w==b.w)
return a.h<b.h;
else
return a.w>b.w;
}
void solve()
{
for(int i=;i<=n;i++)
{
d[][i]=p[].w*p[i].h;
w[][i]=; } for(int i=;i<=k;i++)
{
w[i][n+]=n;
for(int j=n;j>=i;j--)
{
d[i][j]=inf;
for(int s=w[i-][j];s<=w[i][j+];s++)
{
if(d[i][j]>(d[i-][s]+p[s+].w*p[j].h) )
{
d[i][j]=(d[i-][s]+p[s+].w*p[j].h);
w[i][j]=s;
}
}
}
}
/*for(int i=0;i<=k;i++)
{
for(int j=0;j<=n+1;j++)
printf("%d ",w[i][j]);
printf("\n");
}
printf("\n");
for(int i=0;i<=k;i++)
{
for(int j=0;j<=n;j++)
printf("%lld ",d[i][j]);
printf("\n");
}*/
printf("%lld\n",d[k][n]); }
int main()
{
while(~scanf("%d%d",&n,&k))
{
//init();
memset(d,,sizeof(d));
for(int i=;i<=n;i++)
scanf("%lld%lld",&p[i].w,&p[i].h);
sort(p+,p+n+,cmp); int j=;
for(int i=;i<=n;i++)
{
if(p[i].h<p[j].h)
continue;
else
{
p[++j]=p[i];
}
}
n=j; solve();
}
return ;
}
hdu 3669(斜率优化DP)的更多相关文章
- HDU 4258 斜率优化dp
Covered Walkway Time Limit: 30000/10000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 2829 斜率优化DP Lawrence
题意:n个数之间放m个障碍,分隔成m+1段.对于每段两两数相乘再求和,然后把这m+1个值加起来,让这个值最小. 设: d(i, j)表示前i个数之间放j个炸弹能得到的最小值 sum(i)为前缀和,co ...
- hdu 3045 斜率优化DP
思路:dp[i]=dp[j]+sum[i]-sum[j]-(i-j)*num[j+1]; 然后就是比较斜率. 注意的时这里j+t<=i: #include<iostream> #in ...
- Print Article HDU - 3507 -斜率优化DP
思路 : 1,用一个单调队列来维护解集. 2,假设队列中从头到尾已经有元素a b c.那么当d要入队的时候,我们维护队列的下凸性质, 即如果g[d,c]<g[c,b],那么就将c点删除.直到找到 ...
- HDU 3507 斜率优化dp
Print Article Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- HDU 3507斜率优化dp
Print Article Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- HDU 3507 斜率优化 DP Print Article
在kuangbin巨巨博客上学的. #include <iostream> #include <cstdio> #include <cstring> #includ ...
- HDU 2993 MAX Average Problem(斜率优化DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给定一个长度为n(最长为10^5)的正整数序列,求出连续的最短为k的子序列平均值的最大 ...
- hdu 2829 Lawrence(斜率优化DP)
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...
随机推荐
- 【Kruscal最小生成树】D. Jungle Roads
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/D [Accepted] #include<iostream> #in ...
- TYVJ P 1214 硬币问题
TYVJ P 1214 硬币问题 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 有n种硬币,面值为别为a[1],a[2],a[3]……a[n],每种都 ...
- Android操作系统架构
Android操作系统架构 Android操作系统整体应用架构 Android系统架构和一些普遍的操作系统差不多,都是采用了分层的架构,从他们之间的架构图看,Android系统架构分为四个层,从高 ...
- delightful world--计蒜客(DFS)
http://nanti.jisuanke.com/t/11067 题目大意: 给你一个n和m 下面是m个字符串和k 每一字符串的长度是n 你要找一个串跟每一个串匹配 有k个字符是和这个串相 ...
- Meteor在手机上运行
在本章中,我们将学习如何在Android设备上运行你的应用程序.最近Meteor刚刚添加此功能适用于Windows操作系统,所以我们需要更新 Meteor 应用到 1.3测试版. 注 在写的时候本教程 ...
- CentOS 7.0安装Zimbra 8.6邮件服务器
Zimbra的核心产品是Zimbra协作套件(Zimbra Collaboration Suite,简称ZCS). 系统:Centos7 ip地址:192.168.127.131 安装前准备 1.关闭 ...
- Project Euler:Problem 61 Cyclical figurate numbers
Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygon ...
- Spark应用远程调试
本来想用Eclipse的.然而在网上找了一圈,发现大家都在说IntelliJ怎样怎样好.我也受到了鼓励,遂决定在这台破机器上鼓捣一次IntelliJ吧. Spark程序远程调试,就是将本地IDE连接到 ...
- 利用wxpython编写GUI
首先建立一个简单的布局合理的图形界面,但是其中按下按键没有事情发生 #encoding=utf-8 __author__ = 'heng' #编写一个GUI import wx app = wx.Ap ...
- PADs 元器件PCB建库
直接看图就好了,上图! 有几点需要记住的: 如果没有datasheet的情况下,与焊盘相比,阻焊大0.1mm,钢网小0.1mm.或者阻焊大0.05mm,钢网等大,具体要看引脚的间距. 焊盘太大,比如1 ...