bzoj1597: [Usaco2008 Mar]土地购买

本文介绍了一种利用斜率优化DP算法解决特定问题的方法,并通过维护一个凸包来实现高效的动态规划过程。文章提供了详细的代码示例,同时推荐了一个关于该主题的深入讲解博客。

这道题...写的第一道斜率优化dp 其实就是把题目转换完维护一个凸包

具体解释推荐一个博客 http://www.cnblogs.com/akhpl/p/6715148.html

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int M=50007;
int read(){
    int ans=0,f=1,c=getchar();
    while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
    while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
    return ans*f;
}
LL x[M],y[M],f[M];
int q[M],n,tot;
struct node{LL x,y;}a[M];
bool cmp(node a,node b){return a.x!=b.x?a.x<b.x:a.y<b.y;}
double slop(int a,int b){return (double)(f[b]-f[a])/(y[a+1]-y[b+1]);}
int main()
{
    n=read();
    for(int i=1;i<=n;i++) a[i].x=read(),a[i].y=read();
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++){
        while(tot&&y[tot]<=a[i].y) tot--;
        x[++tot]=a[i].x; y[tot]=a[i].y;
    }
    int head=0,tail=0;
    for(int i=1;i<=tot;i++){
        while(head<tail&&slop(q[head],q[head+1])<x[i]) head++;
        int v=q[head];
        f[i]=f[v]+y[v+1]*x[i];
        while(head<tail&&slop(q[tail-1],q[tail])>slop(q[tail],i)) tail--;
        q[++tail]=i;
    }
    printf("%lld\n",f[tot]);
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/lyzuikeai/p/6979707.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值