带权区间覆盖[2]

2891
【问题描述】

  给定N个带权半开区间。i号区间覆盖[ai,bi),权重为wi。现在要从中选择一些区间,要求任意一个点都被最多1个区间覆盖,目标是最大化总的权重。

【输入格式】

  第一行包含整数N。
  接下来的N行,每行包含三个整数:ai,bi,wi,表示一个区间的左右端点和权重。

【输出格式】

  一个整数,表示最大权重。

【输入样例】

3
1 3 10
4 6 20
2 5 25

【输出样例】

30

【数据范围】

1 ≤ N ≤ 30000
1≤ ai < bi ≤ 1,000,000,000 , 1 ≤ wi ≤ 100,000。

【来源】

经典问题

离散化就好。

#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
const int maxn=30005;
struct shu
{
    int x,y;
    long long w;
    friend bool operator <(shu a,shu b)
    {
        return a.y<b.y;
    }
}a[maxn];
map<int,int>mp;
map<int,int>::iterator it;

long long n=0,m,d[maxn*2]={0};

int read()
{
    int x=0;
    char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch<='9'&&ch>='0')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x;
}
void init()
{
    m=read();
    for(int i=1;i<=m;i++)
    {
        a[i].x=read();
        a[i].y=read();
        a[i].w=read();
        mp[a[i].x]=1;
        mp[a[i].y]=1;
    }
    for(it=mp.begin();it!=mp.end();it++)
    it->second=++n;
    for(int i=1;i<=m;i++)
    {
        a[i].x=mp[a[i].x];
        a[i].y=mp[a[i].y];
    }
    sort(a+1,a+1+m);
}


int main()
{
    freopen("in.txt","r",stdin);
    init();
    int k=1;
    for(int i=1;i<=n;i++)
    {
        d[i]=d[i-1];
        while(i==a[k].y&&k<=m)
        {
            d[i]=max(d[i],d[a[k].x]+a[k].w);
            k++;
        }
    }
    cout<<d[n];
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值