codeforces 822 C. Hacker, pack your bags!

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

Copy

4 5
1 3 4
1 2 5
5 6 1
1 2 4

output

Copy

5

input

Copy

3 2
4 6 3
2 4 1
3 5 4

output

Copy

-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,要求选择两张时间不相交的旅券时间长度相加为x,且要求花费最少
注意每个旅游的时间为:l-r+1 不相交:ri<lj or rj<li  

思路:
选择两个不相交的区间需满足:前端点的r<后端点的l , 
把一个区间[l,r]分解成两个区间[l,r]和[r,l]加入到vector数组中,并把前一个标记为-1,后一个标记为1,我们只需要按照左端点从小到大排序 
(这样做的意义就是区分原区间的左右端点,并加入到同一个数组中,可以线性处理)
假设x y为我们选择的旅卷,[xl xr] [yl yr] xr<yl 
 每当遇见-1标记的,说明是原区间的左端点,它可以当作y,并找到前面已经记录的 满足相加为x 的最小花费相加,更新最小答案。即ans=min(ans,vis[x-time]+w)(此时前面必定和它不相交)
 每当遇见1标记的,说明是原区间的右端点,更新此旅游时间的最小花费。vis[time]=min(vis[time],w) 

 

#include <cstdio>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <iostream>
#define ll long long
#define INF 0x3f3f3f
using namespace std;
const int N=400000+1000;

struct node
{
    int l,r,dir;
    ll w;
}temp;
vector<node> v;
ll vis[N];
bool cmp(node a,node b)
{
    if(a.l==b.l) return a.dir<b.dir;
    return a.l<b.l;
}
int main()
{
    int n,x,i,l,w,r,time;
    ll ans;
    memset(vis,0,sizeof(vis));
    ans=1e11;
    scanf("%d%d",&n,&x);
    for(i=0;i<n;i++)
    {
        scanf("%d%d%d",&l,&r,&w);
        temp.w=w;
        temp.l=l;temp.r=r;temp.dir=-1;
        v.push_back(temp);
        temp.l=r;temp.r=l;temp.dir=1;
        v.push_back(temp);
    }
    sort(v.begin(),v.end(),cmp);
    for(i=0;i<v.size();i++)
    {
        time=abs(v[i].r-v[i].l)+1;
        if(time>=x) continue;
        if(v[i].dir==-1)
        {
            if(vis[x-time]!=0)
                ans=min(ans,vis[x-time]+v[i].w);///找前面的区间 和为x 的 最小价值
        }
        else
        {
            if(!vis[time]) vis[time]=v[i].w;
            else vis[time]=min(vis[time],v[i].w);
        }
    }
    if(ans==1e11) printf("-1\n");
    else printf("%lld\n",ans);
    return 0;
}





 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值