BZOJ2034 【2009国家集训队】最大收益

题面

题解

第一眼:线段树优化连边的裸题

刚准备打,突然发现:

\(1 \leq S_i \leq T_i \leq 10^8\)

这™用个鬼的线段树啊

经过一番寻找,在网上找到了一篇论文

大家可以去看一下,这里只提示大家用类似匈牙利算法贪心

这里还有代码

代码

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define RG register
#define clear(x, y) memset(x, y, sizeof(x))

inline int read()
{
    int data = 0, w = 1; char ch = getchar();
    while(ch != '-' && (!isdigit(ch))) ch = getchar();
    if(ch == '-') w = -1, ch = getchar();
    while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
    return data * w;
}

const int maxn(5010);
struct node { int l, r, val; } p[maxn];
int n, val[maxn], match[maxn];
long long ans;
inline bool cmp_1(const node &a, const node &b) { return a.l < b.l; }
inline bool cmp_2(const node &a, const node &b) { return a.val > b.val; }

int hungary(int x, int y)
{
    if(val[y] > p[x].r) return 0;
    if(!match[y]) return (match[y] = x, 1);
    if(p[match[y]].r < p[x].r) return hungary(x, y + 1);
    else if(hungary(match[y], y + 1)) return (match[y] = x, 1);
    return 0;
}

int main()
{
    n = read();
    for(RG int i = 1; i <= n; i++) p[i] = (node) {read(), read(), read()};
    std::sort(p + 1, p + n + 1, cmp_1);
    for(RG int i = 1; i <= n; i++) val[i] = std::max(val[i - 1] + 1, p[i].l);
    for(RG int i = 1, j = 1; i <= n; i++)
    {
        while(j < n && val[j] < p[i].l) ++j;
        p[i].l = j;
    }
    std::sort(p + 1, p + n + 1, cmp_2);
    for(RG int i = 1; i <= n; i++)
        if(hungary(i, p[i].l)) ans += p[i].val;
    printf("%lld\n", ans);
    return 0;
}

转载于:https://www.cnblogs.com/cj-xxz/p/10290572.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值