2019.01.26-bzoj2131: 免费的馅饼

题目描述:

算法标签:dp优化

思路:

容易列出式子:f[i]表示收集到第i个馅饼最多收集到的馅饼。

之后列出式子后,有一个abs,把他去掉得到两个条件,形成二维偏序,树状数组维护即可。

以下代码:

#include<bits/stdc++.h>
#define il inline
#define _(d) while(d(isdigit(ch=getchar())))
using namespace std;
const int N=1e5+5;
int n,b[N],tot,g[N],ans,res;
struct node{int v,x,y;}d[N];
il int read(){
   int x,f=1;char ch;
   _(!)ch=='-'?f=-1:f;x=ch^48;
   _()x=(x<<1)+(x<<3)+(ch^48);
   return f*x;
}
bool cmp(node t1,node t2){
    return (t1.x<t2.x||(t1.x==t2.x&&t1.y<t2.y));
}
il void ins(int x,int v){
    for(;x<=n;x+=x&-x)g[x]=max(g[x],v);
}
il int query(int x){
    int ans=0;for(;x;x-=x&-x)ans=max(ans,g[x]);
    return ans;
}
int main()
{
    read();n=read();
    for(int i=1;i<=n;i++){
        int t=read(),p=read();d[i].v=read();
        d[i].x=2*t-p;d[i].y=2*t+p;b[i]=d[i].y;
    }
    sort(b+1,b+1+n);tot=unique(b+1,b+1+n)-b-1;
    sort(d+1,d+1+n,cmp);
    for(int i=1;i<=n;i++){
        int pos=lower_bound(b+1,b+1+tot,d[i].y)-b;
        res=query(pos)+d[i].v;ans=max(ans,res);
        ins(pos,res);
    }
    printf("%d
",ans);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/Jessie-/p/10325610.html