zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Flutter 导航栏上添加搜索按钮
    tabController保活
    nav 选项卡
    flutter 毛玻璃
    [题解]CodeForces878 D
    [题解]CodeForces878C Tournament
    [题解]NOIP2012
    bzoj1070题解
    bzoj1061题解
    bzoj1059题解
  • 原文地址:https://www.cnblogs.com/Zinn/p/9198171.html
Copyright © 2011-2022 走看看