前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #422(Div.2) C. Hacker, pack your bags!

Codeforces Round #422(Div.2) C. Hacker, pack your bags!

作者头像
ACM算法日常
发布2018-08-07 17:08:51
3520
发布2018-08-07 17:08:51
举报
文章被收录于专栏:ACM算法日常ACM算法日常

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.

Examples

Input

Copy

4 5

1 3 4

1 2 5

5 6 1

1 2 4

Output

5

Input

Copy

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.

概译:给出hacker旅行日程,输入n,x。接下来的n行每行给出每个旅行项目的起始时间l,结束时间r,花费cost。而duration的计算式为r-l+1。约束条件是,n个里面只选2个,要求i的duration和j的duration加起来正好是x,且i和j的时间不重叠(严格不重叠),使得花费最小cost。

官方Tutorial题解思路,我自己理解一下:比如当前的是i-th,我们想找到与他匹配的那个,就建立一个bestcost数组作为平台,need=x-(i[right]-i[left]+1);这样bestcost[need]就是用来记录right-left+1恰好等于x-(i[right]-i[left]+1)的、最小的cost值。具体看注释,代码如下:

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;

vector<pair<pair<int, int>, pair<int, int> > >v;
int n, x, a, b, cost;
long long ans = 2e9 + 1; //ans的初始化至少也要大于2倍的cost最大值,因为要选两个
int bestcost[200005];//不超过x的最大值即可

int main()
{
    scanf("%d%d", &n, &x);
    fill(bestcost + 1, bestcost + 200005, 2e9 + 1);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d%d", &a, &b, &cost);
        //把left,-1,right,cost放进去
        v.push_back(make_pair(make_pair(a, -1), make_pair(b, cost)));
        //下面这句的目的会在后面选择时体现,稍安勿躁
        v.push_back(make_pair(make_pair(b, 1), make_pair(a, cost)));
    }

    sort(v.begin(), v.end());
    //对于pair的排序,默认先排first,再排second,这里我们可以看到上面的-1和1不是随意取的,是故意-1<1

    int t = v.size(); //以下以第i个做例子讲解
    for (int i = 0; i < v.size(); i++)
    {
        int type = v[i].first.second;
        //第i个我们一共放了两个,一个a带-1,一个b带1,排序过后肯定先到(a,-1)
        if (type == -1)
        {
            int need = x - (v[i].second.first - v[i].first.first + 1);
            if (need > 0)
                ans = min(ans, 1LL * bestcost[need] + 1LL * v[i].second.second); //加完可能会爆int,所以临时改LL
            //如果还没解锁need的cost也无碍(比如前几项肯定是跟后面的项匹配,可是后面的bestcost还是2e9+1的状态)
        }
        else
        {
            //这里就是之前加了第二句话的目的,用了贪心的策略,使得我们不用再判断ri>lj或者rj<li之类的了
            //之前虽然第i个已经轮过一次了,但只有比他的右端还大的人才能使用它的cost,请思考pair的排序
            //到这里相当于“解锁”第i个的cost一样,从这以后的j项若是求完need是这项匹配,就可以使用了,因为已经j[a]>i[b]了
            int k = v[i].first.first - v[i].second.first + 1;
            bestcost[k] = min(bestcost[k], v[i].second.second);
        }
    }

    printf("%lld", ans > 2e9 ? -1 : ans);
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-05-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 ACM算法日常 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档