C

来源:互联网 发布:java项目重命名 编辑:程序博客网 时间:2024/06/16 10:37
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 exactlyx 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 thatn vouchers left.i-th voucher is characterized by three integersli,ri,costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of thei-th voucher is a valueri - 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 vouchersi andj(i ≠ j) so that they don't intersect, sum of their durations isexactlyx and their total cost is as minimal as possible. Two vouchersi andj don't intersect if only at least one of the following conditions is fulfilled:ri < lj orrj < li.

Help Leha to choose the necessary vouchers!

Input

The first line contains two integers n andx(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 integersli,ri andcosti(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 durationexactlyx.

Examples
Input
4 51 3 41 2 55 6 11 2 4
Output
5
Input
3 24 6 32 4 13 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 be4 + 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 to2.

这又是cf常出的思维题目。

在这种题目上卡了很多次了,哎

这种题目就是固定!固定!

没有固定制造固定

这次是固定了区间的前后。

固定的方式就是排序

就如同等式的求解一样,固定一些数组来求解

思路:

首先我们把给定的数据分成两份,第一份按照出去的时间升序排序,第二份按照回来的时间升序排序,之后遍历第一份,这样可以根据第一份的出去的时间来决定第二份回来的时间,然后维护一个最小花费数组mincost,对于每一个数据求出t=x-(r-l+1),然后更新最小值就可以了。

#include<bits/stdc++.h>using namespace std;const int M=2e5+10;struct aa{    int u,v,w;} save1[M],save2[M];long long mincost[M];bool cmp1(const aa &a,const aa &b){    return a.u<b.u;}bool cmp2(const aa &a,const aa &b){    return a.v<b.v;}long long gmin(long long a,long long b){return a>b?b:a;}int main(){    int n,x;    while(scanf("%d%d",&n,&x)!=EOF)    {        for(int i=0;i<M;i++)        mincost[i]=INT_MAX;        for(int i=0; i<n; i++)        {            scanf("%d%d%d",&save1[i].u,&save1[i].v,&save1[i].w);            save2[i]=save1[i];        }        sort(save1,save1+n,cmp1);        sort(save2,save2+n,cmp2);        int minn=INT_MAX;        int j=0;        for(int i=0; i<n; i++)        {            while(j<n&&save2[j].v<save1[i].u)            {                mincost[save2[j].v-save2[j].u+1]=gmin(mincost[save2[j].v-save2[j].u+1],save2[j].w);//维护最小值                j++;            }            int k=x-(save1[i].v-save1[i].u+1);//找出对应时间            if(k>0&&minn>mincost[k]+save1[i].w)//更新最小值                minn=mincost[k]+save1[i].w;        }        if(minn==INT_MAX)            printf("-1\n");        else            printf("%d\n",minn);    }}


原创粉丝点击