bzoj3963 [WF2011]MachineWorks(斜率优化 & splay维护动态凸包)

83 篇文章 0 订阅
51 篇文章 1 订阅

bzoj3963 [WF2011]MachineWorks

原题地址http://www.lydsy.com/JudgeOnline/problem.php?id=3963

题意:
多组数据。
你是任意性复杂机器公司(Arbitrarily Complex Machines, ACM)的经理,公司使用更加先进的机械设备生产先进的机器。原来的那一台生产机器已经坏了,所以你要去为公司买一台新的生产机器。你的任务是在转型期内尽可能得到更大的收益。在这段时间内,你要买卖机器,并且当机器被ACM公司拥有的时候,操控这些机器以获取利润。因为空间的限制,ACM公司在任何时候都只能最多拥有一台机器
在转型期内,有若干台可能卖出的机器。对于每台机器Mi,你已经知道了其价格Pi和可以买入的日期Di。注意,如果不在第Di天买入机器Mi,那么别的人也会买走这一台机器,也就是说,以后你将没有机会购买这台机器了。如果ACM的钱低于一台机器的价格,那么你显然不可能买到这一台机器
如果你在第Di天买入了机器Mi,那么ACM公司可以从第(Di)+1天开始使用这一台机器。每使用这台机器一天,就可以为公司创造出Gi美元的收益。
你可以决定要在买入之后的某一天,以一定的折扣价卖出这一台机器。收购市场对于每一台机器,都有一个折扣价Ri。你不能在卖出的那一天使用机器,但是你可以在卖出的那一天再买入一台新的。
在转型期结束后,ACM公司会卖掉当前所拥有的机器。你的任务就是最大化转型期间ACM公司可以得到的收入。

数据范围
1<=Di<=D,1<=Ri< Pi<=10^9,1<=Gi<=10^9.

题解:
f(i)=f(j)Pj+Rj+Gj(DiDj1)
DiGj+f(i)=f(j)Pj+RjGj(Dj+1)
若令 x=Gj ,令 y=f(j)Pj+RjGj(Dj+1) k=Di
又变成了截距的形式。
x 不单调,k,用splay来维护动态凸包。

注意:只有f[i]>=P[i]的才能加入凸包中
   erase时记得改fa。

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
/*
    -Di*Gj+f(i)=f(j)-Pj+Rj-Gj*(Dj+1)
    x=Gj  y=f(j)-Pj+Rj-Gj*(Dj+1)
    只有f[i]>=P[i]的才能加入凸包中
 */
const int N=100005;
int cas=0,n,c,d,root=0;
LL f[N];
long double lk[N],rk[N];
bool exi[N];
inline int read()
{
    int ret=0; int w=1; char ch=getchar();
    while((ch>'9'||ch<'0')&&ch!='-') ch=getchar();
    if(ch=='-') {w=-1,ch=getchar();}
    while(ch>='0'&&ch<='9')  ret=(ret<<1)+(ret<<3)+(ch^'0'),ch=getchar();
    return ret*w;
}
struct MC
{
    LL D,P,R,G;
    MC(){}
    MC(LL D,LL P,LL R,LL G):D(D),P(P),R(R),G(G){}
}a[N];
struct node
{
    int ch[2],fa;
    LL x,y;
    void init(int i) {x=a[i].G; y=f[i]-a[i].P+a[i].R-a[i].G*(a[i].D+1LL); ch[0]=ch[1]=fa=0;}
}tr[N];
inline bool cmpD(const MC &A,const MC &B) {return A.D<B.D;}
void rotate(int x,int &top)
{
    int y=tr[x].fa; int z=tr[y].fa;
    if(y==top) top=x;
    else {if(tr[z].ch[0]==y) tr[z].ch[0]=x; else tr[z].ch[1]=x;}
    tr[x].fa=z;
    int l=(tr[y].ch[0]==x)?0:1; int r=l^1;
    tr[y].ch[l]=tr[x].ch[r]; tr[tr[x].ch[r]].fa=y;
    tr[x].ch[r]=y; tr[y].fa=x;
}
void splay(int x,int &top)
{
    while(x!=top)
    {
        int y=tr[x].fa; int z=tr[y].fa;
        if(y!=top)
        {
            if((tr[y].ch[0]==x)^(tr[z].ch[0]==y)) rotate(x,top);
            else rotate(y,top);
        }
        rotate(x,top);
    }
}
void insert(int &nd,int x,int f)
{
    if(!nd){nd=x; tr[x].fa=f; splay(x,root); return;}
    if(tr[x].x<=tr[nd].x) insert(tr[nd].ch[0],x,nd);
    else insert(tr[nd].ch[1],x,nd);
}
inline long double getk(int A,int B) 
{
    if(tr[A].x==tr[B].x) return tr[B].y>tr[A].y?1e20:-1e20;
    return (long double)(tr[B].y-tr[A].y)/(tr[B].x-tr[A].x);
}
inline int getpre(int x)
{
    int ret=0; int tmp=tr[x].ch[0];
    while(tmp)
    {
        if(lk[tmp]>=getk(tmp,x)) {ret=tmp; tmp=tr[tmp].ch[1];}
        else tmp=tr[tmp].ch[0];
    }
    return ret;
}
inline int getnxt(int x)
{
    int ret=0; int tmp=tr[x].ch[1];
    while(tmp)
    {
        if(rk[tmp]<=getk(x,tmp)) {ret=tmp; tmp=tr[tmp].ch[0];}
        else tmp=tr[tmp].ch[1];
    }
    return ret;
}
void erase(int x)
{
    if(!tr[x].ch[0]) {root=tr[x].ch[1]; return;}
    if(!tr[x].ch[1]) {root=tr[x].ch[0]; return;}
    int pre=tr[x].ch[0]; int nxt=tr[x].ch[1]; tr[root].fa=0; tr[nxt].fa=pre;
    root=pre; tr[root].ch[1]=nxt;
    rk[pre]=lk[nxt]=getk(pre,nxt);
    exi[x]=0;
}
void Add(int x)
{
    if(f[x]<a[x].P) return;
    tr[x].init(x);
    exi[x]=1;
    insert(root,x,0);
    if(tr[x].ch[0])
    {
        int pre=getpre(x);
        splay(pre,tr[x].ch[0]);
        tr[pre].ch[1]=0;
        rk[pre]=lk[x]=getk(pre,x);
    }
    else lk[x]=1e20;
    if(tr[x].ch[1])
    {
        int nxt=getnxt(x);
        splay(nxt,tr[x].ch[1]);
        tr[nxt].ch[0]=0;
        rk[x]=lk[nxt]=getk(x,nxt);
    }
    else rk[x]=-1e20;
    if(lk[x]<=rk[x]) erase(x);  
}
inline int find(int nd,long double k)
{
    int tmp=nd; 
    while(tmp)
    {
        if(lk[tmp]>=k&&rk[tmp]<k) return tmp;
        else if(rk[tmp]>=k) tmp=tr[tmp].ch[1];
        else tmp=tr[tmp].ch[0];
    }
    return tmp;
}
int main()
{
    while(1)
    {
        cas++; n=read(); c=read(); d=read();
        if(!n&&!c&&!d) break;
        for(int i=1;i<=n;i++){a[i].D=read();a[i].P=read();a[i].R=read();a[i].G=read();}
        a[++n]=MC(d+1,0,0,0);
        sort(a+1,a+n+1,cmpD); f[0]=1LL*c; LL ret=0; root=0;
        for(int i=1,tmp=1;i<=n;i++)
        {
            while(tmp<=n&&a[tmp].D<a[i].D) {Add(tmp); tmp++;}
            f[i]=f[i-1];
            if(root) 
            {
                int j=find(root,(long double)-a[i].D);
                f[i]=max(f[i],f[j]-a[j].P+a[j].R+a[j].G*(a[i].D-a[j].D-1LL));
            }
            ret=max(ret,f[i]);
        }
        printf("Case %d: %lld\n",cas,ret);
    }       
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值