bzoj1597 [Usaco2008 Mar]土地购买——斜率优化DP

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1597

就是斜率优化水题...

然而WA了十几遍,正负号处理真让人心累...

还是该负就负,别乱换了...

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const maxn=50005;
int n,cnt,q[maxn],h,t;
ll f[maxn],x[maxn],y[maxn];
struct N{ll a,b;}p[maxn];
ll rd()
{
    ll ret=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();
    return ret*f;
}
//bool cmp(N x,N y){return x.a==y.a?x.b>y.b:x.a<y.a;}
bool cmp(N x,N y){return x.a==y.a?x.b>y.b:x.a<y.a;}
double slp(int a,int b){return (double)(f[b]-f[a])/(double)(y[a+1]-y[b+1]);}
int main()
{
    n=rd();
    for(int i=1;i<=n;i++)p[i].a=rd(),p[i].b=rd();
    sort(p+1,p+n+1,cmp);
    for(int i=1;i<=n;i++)
    {
//        if(p[i].a==p[i-1].a)continue;
//        x[++cnt]=p[i].a; y[cnt]=p[i].b;
        while (cnt &&  p[i].b>=y[cnt]) cnt--;
        x[++cnt]=p[i].a; y[cnt]=p[i].b;
    }
//    h=1;t=1;//
    for(int i=1;i<=cnt;i++)
    {
        while(h<t&&slp(q[h],q[h+1])<x[i])h++;//<
        f[i]=f[q[h]]+x[i]*y[q[h]+1];
        while(h<t&&slp(q[t-1],q[t])>slp(q[t],i))t--;
        q[++t]=i;
    }
    printf("%lld",f[cnt]);
    return 0;
}

 

转载于:https://www.cnblogs.com/Zinn/p/9198171.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值