单调栈——cf777E

傻逼单调栈啊我怎么想了半天dp

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef struct P {
    LL a, b, h;
    P() {}
    P(LL a, LL b, LL h) : a(a), b(b), h(h) {}
}P;
const LL maxn = 100100;
LL n;
P p[maxn];

bool cmp(P x, P y) {
    if(x.b == y.b) return x.a > y.a;
    return x.b > y.b;
}
stack<P> st;

signed main() {
    // freopen("in", "r", stdin);
    while(~scanf("%I64d", &n)) {
        for(LL i = 1; i <= n; i++) {
            scanf("%I64d%I64d%I64d",&p[i].a,&p[i].b,&p[i].h);
        }
        sort(p+1, p+n+1, cmp);
        while(!st.empty()) st.pop();
        LL ret = 0;
        for(LL i = 1; i <= n; i++) {
            while(!st.empty() && p[i].b <= st.top().a) st.pop();
            if(!st.empty()) p[i].h += st.top().h;
            st.push(p[i]);
            ret = max(ret, st.top().h);
        }
        printf("%I64d\n", ret);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zsben991126/p/11020597.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值