Codeforces 822C - Hacker, pack your bags!(二分)

本文介绍了一道关于旅行计划的问题,需要将假期分为两部分,通过算法寻找最优的旅行组合方案,确保旅行不冲突且总费用最低。

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

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.


题目大意:
某人要进行休假,他想将总的休假时间分成两部分去进行两次旅行,有n个可供选择的旅行,每一个旅行有三个参数,l,r,cost,分别代表旅行的开始时间,旅行的结束时间,旅行的花费,旅行花费的天数是r-l+1,要求找出两段旅行,时间不交叉,且相加的时间是x。
题解:
首先对旅行按照时长分开,开vector保存,然后根据旅行的结束时间排序,处理前缀最小花费为cst。然后枚举每一段旅行,参数为l0,r0,cost0,然后在满足时长为x-(r0-l0+1)中二分查找满足r<l0的最大下标,判定ans=min(cst+cost0,ans),排序最大复杂度O(nlongn),前缀最小值复杂度O(n),枚举二分过程O(nlogn),所以总复杂度O(nlogn);
代码如下:
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

struct vca
{
    long long l,r;
    long long cost;
    bool operator < (const vca a)const
    {
        return l<a.l;
    }
};

bool cmp(vca a,vca b)
{
    return a.r<b.r;
}

vector<vca> ll[200005];
vector<long long> mp[200005];

long long binsearch(long long i,long long ss)
{
    long long len=ll[i].size();
    long long l=0,r=len-1;
    if(len==0)
        return -1;
    while(l<=r)
    {
        long long mid=(l+r)>>1;
        if(ll[i][mid].r>=ss)
            r=mid-1;
        else
            l=mid+1;
    }
    return r;
}

int main()
{
    long long n,x;
    cin>>n>>x;
    vca now;
    for(long long i=0;i<n;i++)
    {
        long long l,r,cc;
        scanf("%lld %lld %lld",&l,&r,&cc);
        now.l=l;
        now.r=r;
        now.cost=cc;
        ll[r-l+1].push_back(now);
    }
    for(long long i=0;i<x;i++)
    {
        long long l=ll[i].size();
        if(l==0)
            continue;
        //mp[i].push_back(ll[i][0].cost)
        sort(ll[i].begin(),ll[i].end(),cmp);
        mp[i].push_back(ll[i][0].cost);
        for(long long j=1;j<l;j++)
        {
            mp[i].push_back(min(mp[i][j-1],ll[i][j].cost));
        }
        //sort(ll[i].begin(),ll[i].end(),cmp);
    }
    long long ans=20000000005;
    for(long long i=0;i<x;i++)
    {
        long long l=ll[i].size();
        for(long long j=0;j<l;j++)
        {
            long long c=binsearch(x-i,ll[i][j].l);
            if(c<0)
                continue;
            ans=min(ll[i][j].cost+mp[x-i][c],ans);
        }
    }
    if(ans==20000000005)
        cout<<"-1"<<endl;
    else
    cout <<ans<< endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值