Codeforces822 C. Hacker, pack your bags!

时间:2023-03-10 02:30:25
Codeforces822 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 li,ricosti — 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 orrj < 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 to2.

——————————————————————————————————

题目的意思是给出n个区间,选出两个不相交的区间,区间长度和要等于m,求最小花费

思路:按l排序,枚举区间作为选择的后半段,开始一个数组xx保存长度为i的花费最小值,用一个优先队列维护已处理的区间的所达到的xx。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std; #define LL long long
const LL INF = 0x7fffffffffffffff; struct node
{
int l,r;
LL c;
friend bool operator<(node a,node b)
{
return a.r>b.r;
}
} a[200005];
LL xx[200005]; bool cmp(node a,node b)
{
if(a.l!=b.l)
return a.l<b.l;
return a.c<b.c;
} int main()
{
int n,x;
scanf("%d%d",&n,&x);
for(int i=0; i<n; i++)
{
scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].c);
}
sort(a,a+n,cmp);
LL mn=INF;
priority_queue<node>q;
memset(xx,-1,sizeof xx);
for(int i=0; i<n; i++)
{
int ll=a[i].r-a[i].l+1;
if(ll>=x)
continue;
while(!q.empty()&&q.top().r<a[i].l)
{
int xs=q.top().r-q.top().l+1;
if(xx[xs]==-1)
xx[xs]=q.top().c;
xx[xs]=min(xx[xs],q.top().c);
q.pop();
}
q.push(a[i]);
if(xx[x-ll]==-1)
continue;
mn=min(mn,a[i].c+xx[x-ll]); }
printf("%lld\n",mn==INF?-1:mn);
return 0;
}