CodeForces - 822C Hacker, pack your bags!(贪心)

本文解析了CodeForces平台上的一道C级题目,通过贪心算法解决如何选择两个不交集的旅行券以达到指定天数的问题。文章介绍了输入输出格式及样例,并给出了实现代码。

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


题目链接:http://codeforces.com/problemset/problem/822/C点击打开链接



C. Hacker, pack your bags!
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.

So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers liricosti — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.

At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.

Help Leha to choose the necessary vouchers!

Input

The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.

Each of the next n lines contains three integers liri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.

Output

Print a single integer — a minimal amount of money that Leha will spend, or print  - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.

Examples
input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
output
5
input
3 2
4 6 3
2 4 1
3 5 4
output
-1
Note

In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5and the total cost will be 4 + 1 = 5.

In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.



贪心题 让你求选其中的两个旅游圈 价值总和为x且天数之和等于n 

想了好久不会写 借鉴其他博客的想法写的

用两个数组记录 然后按出发时间升序 到达时间升序排列

便利第一个数组 然后如果第二个序列能作为第一张卷 就用数组记录他的值(前提取最小) 不能将第二序列的指针往下;

遍历完第一个数组就能够取到最小值

#include<bits/stdc++.h>
using namespace std;
struct xjy
{
    int l;
    int r;
    int val;
}a[222222],b[222222];
int ans[222222];
int cmp1(xjy &a,xjy &b)
{
    return a.l<b.l;
}
int cmp2(xjy &a,xjy &b)
{
    return a.r<b.r;
}
int main()
{
    int n,m;int aans=INT_MAX;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
        ans[i]=INT_MAX;
    for(int i=0;i<n;i++)
    {
        xjy mid;
        scanf("%d%d%d",&mid.l,&mid.r,&mid.val);
        a[i]=b[i]=mid;
    }
    sort(a,a+n,cmp1);
    sort(b,b+n,cmp2);
    int temp=0;
    for(int i=0;i<n;i++)
    {
        while(temp<n&&b[temp].r<a[i].l)
        {
            ans[b[temp].r-b[temp].l+1]=min(ans[b[temp].r-b[temp].l+1],b[temp].val);
            temp++;
        }
        if(m-(a[i].r-a[i].l+1)>0&&aans>ans[m-(a[i].r-a[i].l+1)])
            aans=min(ans[m-(a[i].r-a[i].l+1)]+a[i].val,aans);
    }
    if(aans==INT_MAX)
        printf("-1");
    else
        printf("%d",aans);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值