bzoj1597 [Usaco2008 Mar]土地购买

题目描述

题解:

我们可以考虑这样一种情况,有一块$10*10$的土地,还有一块$1*1$的土地。

当然将$1*1$的土地合到$10*10$的土地中了。

所以我们可以先按土地$x$值从大到小排序,然后从前向后扫一边,只将不会被完全覆盖的放到处理的集合里。

这样就可以$dp$了。

有$$dp[i]=min(dp[j-1]+x[j]*y[i])$$

然后$$dp[j-1]=y[i]*(-x[j])+dp[i]$$

我们就可以进行开心的斜率优化了。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 50050
#define ll long long
inline ll rd()
{
    ll f=1,c=0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){c=10*c+ch-'0';ch=getchar();}
    return f*c;
}
ll n,m;
struct Pair
{
    ll a,b;
}p0[N],p[N];
bool cmp(Pair x,Pair y)
{
    if(x.a!=y.a)return x.a>y.a;
    return x.b>y.b;
}
ll dp[N];
struct wsx
{
    ll x,y;
    wsx(){}
    wsx(ll x,ll y):x(x),y(y){}
}sta[N];
double slop(wsx a,wsx b)
{
    return (double)(a.y-b.y)/(a.x-b.x);
}
int tl;
int fd(ll k)
{
    int l=1,r=tl-1,ans=tl;
    while(l<=r)
    {
        int mid = (l+r)>>1;
        if((sta[mid+1].y-sta[mid].y)>k*(sta[mid+1].x-sta[mid].x))
        {
            ans = mid;
            r = mid-1;
        }else l = mid+1;
    }
    return ans;
}
int main()
{
//  freopen("tt.in","r",stdin);
    n = rd();
    for(int i=1;i<=n;i++)
        p0[i].a=rd(),p0[i].b=rd();
    sort(p0+1,p0+1+n,cmp);
    ll max_b = -1;
    for(int i=1;i<=n;i++)
    {
        if(p0[i].b>max_b)
        {
            max_b = p0[i].b;
            p[++m] = p0[i];
        }
    }
    sta[++tl] = wsx(-p[1].a,0);
    for(int i=1;i<=m;i++)
    {
        int now = fd(p[i].b);
        dp[i] = sta[now].y - sta[now].x*p[i].b;
        wsx tmp = wsx(-p[i+1].a,dp[i]);
        while(tl>=1&&slop(sta[tl],sta[tl-1])>=slop(sta[tl],tmp))tl--;
        sta[++tl]=tmp;
    }
    printf("%lld\n",dp[m]);
    return 0;
}

 

posted @ 2019-01-04 09:44  LiGuanlin  阅读(122)  评论(0编辑  收藏  举报