NEUOJ 1211 MJF wants to work (与区间相关的贪心)

本文提供了一道2017年ICPCECIC竞赛题目的详细解答过程,通过使用C++实现的一种算法来解决该问题。该算法利用了动态规划思想,并通过结构体数组和自定义排序进行优化,最终解决了区间覆盖问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

2017ICPCECIC H题

题目大意

分析

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
const int INF=0x3F3F3F3F;
const int MAXN=200005;
using namespace std;
struct Dot
{
    int x,flag,time,money;
}a[MAXN*2];

bool cmp(struct Dot a,struct Dot b)
{
    return (a.x<b.x||((a.x==b.x)&&(!a.flag)&&(b.flag)));
}
int main()
{
    int dp[MAXN],n,m,i,l,r,c,ans;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        ans=INF;
        for (i=1;i<=n;i++)
        {
            scanf("%d%d%d",&l,&r,&c);
            a[2*i-1].x=l;
            a[2*i-1].flag=0;
            a[2*i-1].time=r-l+1;
            a[2*i-1].money=c;
            a[2*i].x=r;
            a[2*i].flag=1;
            a[2*i].time=r-l+1;
            a[2*i].money=c;
        }
        sort(a+1,a+2*n+1,cmp);
        memset(dp,INF,sizeof(dp));
        for (i=1;i<=2*n;i++)
        {
            if (!a[i].flag)
                ans=min(ans,dp[m-a[i].money]+a[i].time);
            else
                dp[a[i].money]=min(dp[a[i].money],a[i].time);
        }
        if (ans==INF)
            printf("oh no!\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值