E. Hanoi Factory(dp+单调性优化/dp+线段树)

56 篇文章 1 订阅
43 篇文章 0 订阅

https://codeforces.com/problemset/problem/777/E


思路:

推出O(n^2)的转移方程,然后优化。

对于优化有两种,一种考虑到决策的单调性,可以利用优先队列或者单调栈 进行优化。另一种是用数据结构的方法来直接找符合要求的转移最值。

先第一种。

每次都要找最大值,考虑用堆来实现。但是有一个转移条件bi​>aj​。

我们每次取堆头是都要判断一下这个转移是否合法,如果不合法就把堆顶弹掉。

这样贪心对吗,会不会把对之后更新有影响的元素弹掉?

考虑当前堆顶dp状态对应的a[j],假如i此刻无法通过他转移,即a[j]>b[i]。那么我们提前按不升序排好了b,对于i位置后的bi,更不可能转移到了。因此直接扔掉就好。

然后在堆里存一下dp值和该dp对应的a[j],重载成小根堆取最大值。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL dp[maxn];
struct P{
   LL a,b,h;
}p[maxn];
bool cmp(P A,P B){
     if(A.b==B.b) return A.a>B.a;
     return A.b>B.b;
}
struct Node{
    LL a,val;
    friend bool operator<(struct Node A,struct Node B){
       return A.val<B.val;
    }
};priority_queue<Node>que;
int main(void){
    LL n;n=read();
    for(LL i=1;i<=n;i++){
        p[i].a=read();p[i].b=read();p[i].h=read();
    }
    sort(p+1,p+1+n,cmp);
    que.push({0,0});
    LL ans=0;
    for(LL i=1;i<=n;i++){
        while(!que.empty()&&que.top().a>=p[i].b) que.pop();
        Node now=que.top();
        dp[i]=max(dp[i],now.val+p[i].h);
        que.push({p[i].a,dp[i]});
        ans=max(ans,dp[i]);
    }
    cout<<ans<<"\n";
   	return 0;
}

另外那一部分可以用权值线段树处理。最后跑了140ms.也很优秀。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=3e5+1000;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
struct Tree{
    LL l,r;long long maxval;
}tree[maxn*4];
LL pos[maxn],m=0;
long long dp[maxn];
struct P{
    LL a,b,h;
}o[maxn];
bool cmp(P A,P B){
     if(A.b==B.b) return A.a>B.a;
     return A.b>B.b;
}
void push_up(LL p){
     tree[p].maxval=max(tree[p*2].maxval,tree[p*2+1].maxval);
}
void build(LL p,LL l,LL r){
     tree[p].l=l;tree[p].r=r;tree[p].maxval=0;
     if(tree[p].l==tree[p].r){return;}
     LL mid=(l+r)>>1;
     build(p*2,l,mid);build(p*2+1,mid+1,r);
     push_up(p);
}
void modify(LL p,LL l,LL r,long long d){
     if(tree[p].l==tree[p].r){
        tree[p].maxval=max(tree[p].maxval,d);
        return;
     }
     LL mid=(tree[p].l+tree[p].r)>>1;
     if(l<=mid) modify(p*2,l,r,d);
     if(r>mid) modify(p*2+1,l,r,d);
     push_up(p);
}
long long query(LL p,LL l,LL r){
     if(l<=tree[p].l&&r>=tree[p].r){
        return tree[p].maxval;
     }
     LL mid=(tree[p].l+tree[p].r)>>1;
     long long ans=0;
     if(l<=mid) ans=max(ans,query(p*2,l,r));
     if(r>mid) ans=max(ans,max(tree[p*2].maxval,query(p*2+1,l,r)));
     return ans;
}
int main(void){
    LL n;n=read();
    for(LL i=1;i<=n;i++){
        LL a,b,h;a=read();b=read();h=read();
        pos[++m]=a;pos[++m]=b;pos[++m]=h;
        o[i].a=a;o[i].b=b;o[i].h=h;
    }
    sort(pos+1,pos+1+m);
    m=unique(pos+1,pos+1+m)-pos-1;
    sort(o+1,o+1+n,cmp);
    build(1,1,m);
    long long ans=0;
    for(LL i=1;i<=n;i++){
        LL inr=lower_bound(pos+1,pos+1+m,o[i].a)-pos;
        LL otr=lower_bound(pos+1,pos+1+m,o[i].b)-pos;
        long long val=query(1,1,otr-1);
        dp[i]=max(dp[i],val+o[i].h);
        ans=max(ans,dp[i]);
        modify(1,inr,inr,dp[i]);
    }
    printf("%lld\n",ans);
   	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值