【DP】【单调队列】17.6.3 上网 题解

这里写图片描述
这里写图片描述
Normal: f[i]=max{f[i-1],f[x-1]+c[x,i]} f[0]=0
c[x,i]表示x玩到i的快乐值
Rare: 加单调队列优化

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define N 500100
#define M 300000

struct node{int x,y,c;} e[M];
int n,T,tot,tree[N];

inline void read(int &x)
{
    x=0; char ch; while(ch=getchar(),ch>'9' || ch<'0');
    while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
}
inline int Max(int x,int y){return x>y?x:y;}
inline bool cmp(node A,node B){ return A.y<B.y; }
inline void insert(int x,int y){
    for(;x<=T;x+=x&-x) tree[x]=Max(tree[x],y);
}
inline int Query(int x){
    int ret=0;
    for(;x;x-=x&-x) ret=Max(ret,tree[x]);
    return ret;
}
int main()
{
    freopen("net.in","r",stdin);
    freopen("net.out","w",stdout);

    read(n); read(T);
    for(int i=1,m;i<=n;i++)
    {
        read(m);
        for(;m--;) {
            ++tot;
            read(e[tot].x);
            read(e[tot].y);
            read(e[tot].c);
        }
    }
    sort(e+1,e+1+tot,cmp);
    for(int i=1,tmp;i<=tot;i++)
    {
        tmp=Query(e[i].x-1)+e[i].c;
        if(tmp>Query(e[i].y))
        insert(e[i].y,tmp);
    }
    printf("%d\n",Query(T));
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值