Hacker, pack your bags!

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 li, ri, costi — 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 li, ri 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.

Example
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) = 5 and 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,且要求花费最少。
【思路】用for去枚举第一个符合题意的区间,再用二分去找第二个符合题意的区间。

   因为要二分,所以要区间有序,首先要确定排序策略:1.按照区间长度由小到大(相反也可
 以)排序 2.若区间长度相等,按照右端点由小到大排序。因为若满足 p[i].r>p[j].l 的话,可
 以得到此时的区间没有交集,最外层的循环是从 0 到 n,每个数都跑一遍,可以看 p[i]之后
符合题意的区间,最外层循环会枚举到每一种情况(两者一前一后,枚举到后面时,自然会
找到前面的)。

如何二分?
用 x-p[i].c 得到要找的值,若是有多个符合条件的值如何处理?
因为不用扫 P[i]后符合条件的值,所以只要求的 p[i]前符合条件的最小值,排序时按 r 从小
到大排序就在此时得到应用,前面的只需要找到最接近 p[i]的区间,维护最小值可以在排序
之后,for 循环维护。



#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 1e18 // long long 的最大值
ll n,x;
struct node{
    int l,r,c,value,cost; // 分别储存区间端点,区间长度,区间价值,前缀最小值
}p[10000005];
int cmp(node a,node b) //结构体排序
{
    if(a.c==b.c)
        return a.r<b.r;
    return a.c<b.c;
}
int main()
{
    int n,x,temp,i,j;
    ll minn;
    while(scanf("%d%d",&n,&x)!=EOF)
    {
        minn=inf;
        for(i=0;i<n;i++)
            scanf("%d%d%d",&p[i].l,&p[i].r,&p[i].value);
        for(i=0;i<n;i++)
            p[i].c=p[i].r-p[i].l+1; //求出左右端点间距 要记得+1
        for(i=0;i<n;i++)
            p[i].cost=p[i].value; // 为下面求cost的值作准备。
        sort(p,p+n,cmp);
        for(i=1;i<n;i++)
        {
            if(p[i].c==p[i-1].c)
                p[i].cost=min(p[i].cost,p[i-1].cost); //若间距相同则让处于最右边的结构体存最小花费。
        }
        for(i=0;i<n;i++)
        {
            if(p[i].c>=x)
                continue;
            ll temp=x-p[i].c;
            int l=0,r=n-1,mid;
            while(l<=r)
            {
                mid=(l+r)/2;
                if(p[mid].c<temp)
                    l=mid+1;
                else if(p[mid].c>temp)
                    r=mid-1;
                else
                {
                    if(p[mid].r>=p[i].l) //只需要找 for循环枚举的到结构体的右边即可
                        r=mid-1;
                    else
                        {
                            minn=min(minn,p[mid].cost+(ll)p[i].value);
                            l=mid+1;
                        }
                }
            }
        }
        if(minn==inf)
            printf("-1\n");
        else
            printf("%lld\n",minn);
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值