zoukankan      html  css  js  c++  java
  • 【BZOJ2726】【SDOI2012】—任务安排(斜率优化dp)

    传送门

    有一个显然的O(n2)dpO(n^2)dp

    f[i]f[i]表示前ii天的最小花费
    tottot是费用的前缀和
    tt是时间的前缀和

    f[i]=min(f[j]+(totntotj)(s+titj))f[i]=min(f[j]+(tot_n-tot_j)*(s+t_i-t_j))
    g[i]=f[i]totnti+totitistotjg[i]=f[i]-tot_n*t_i+tot_i*t_i-s*tot_j

    考虑2个决策点如果xxyy更优
    g[x]g[y]totxtotytifrac{g[x]-g[y]}{tot_x-tot_y}le ti

    那就变成斜率优化的套路了

    注意斜率不要直接除,精度要爆炸的
    交叉相乘判大小

    BZOJBZOJdoubledouble很友好?比long longlong long快了接近一倍

    #include<bits/stdc++.h>
    using namespace std;
    const int RLEN=1<<20|1;
    #define ll long long
    #define re register
    inline char gc(){
    	static char ibuf[RLEN],*ob,*ib;
    	(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    	return (ob==ib)?EOF:*ib++;
    }
    inline int read(){
        char ch=gc();
        int res=0,f=1;
        while(!isdigit(ch))f^=(ch=='-'),ch=gc();
        while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
        return f?res:-res;
    }
    const int N=300005;
    const double eps=1e-6;
    struct point{
        double x,y;
        point(double _x=0,double _y=0):x(_x),y(_y){}
        friend inline point operator -(const point &a,const point &b){
            return point(a.x-b.x,a.y-b.y);
        }
        inline double slope(){
            return x/y;
        }
    }p[N];
    int n,stk[N],top;
    ll f[N],s,tot[N],t[N];
    inline void dp(int i,int j){
        f[i]=f[j]+(t[i]-t[j]+s)*(tot[n]-tot[j]);
    }
    signed main(){
        n=read(),s=read();
        for(re int i=1;i<=n;++i)t[i]=t[i-1]+read(),tot[i]=tot[i-1]+read();
        for(re int i=1;i<=n;++i){
            re int l=1,r=top,res=0;
            while(l<=r){
                int mid=(l+r)>>1;
                point tp=(p[stk[mid]]-p[stk[mid-1]]);
                if(tp.y<=t[i]*tp.x)l=mid+1,res=mid;
                else r=mid-1;
            }
            dp(i,stk[res]);
            p[i].x=tot[i],p[i].y=f[i]-tot[n]*t[i]+tot[i]*t[i]-s*tot[i];
            while(top&&(p[stk[top]]-p[stk[top-1]]).y*(p[i]-p[stk[top]]).x>=(p[i]-p[stk[top]]).y*(p[stk[top]]-p[stk[top-1]]).x)top--;
            stk[++top]=i;
        }
        cout<<f[n];
    }
    
  • 相关阅读:
    保护你的网站数字证书私钥
    Web安全防御从WAF到应用网关
    微信公众号【网络安全生命周期】部分文章目录
    一个SQL Injection漏洞在SDL流程中的闯关历险记
    Web Vulnerability Scanner 破解
    Web安全实战演练1-4
    转:WebCruiser Web Vulnerability Scanner 3 测评
    WAVSEP在Linux系统中部署时常见错误
    css属性操作
    前端基础CSS规则
  • 原文地址:https://www.cnblogs.com/stargazer-cyk/p/11145570.html
Copyright © 2011-2022 走看看