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 isexactly 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!
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.
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 durationexactly x.
4 5 1 3 4 1 2 5 5 6 1 1 2 4
5
3 2 4 6 3 2 4 1 3 5 4
-1
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.
思路 : 1 为了解决两段 , sum = s1 + s2 有一段 便能得到另一段的要求。
2 dp解决 最小费用问题。这里用map 解决。
3 为了防止重叠 , 将线段分解成两个值,然后按照 下标小 ,同下标左优先的顺序排序
这样每次遇到右端点就能更新这段线段长的花费,遇到左端点更新结果。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <map>
#define maxn 200005
using namespace std;
typedef struct node{
int index,mark,value,cost;
}node;
node a[maxn << 1];
bool cmp(node x,node y)
{
if(x.index != y.index)
return x.index < y.index;
return x.mark < y.mark;
}
int main(){
int n, x;
scanf("%d%d", &n, &x);
for(int i = 0;i < 2*n; i+= 2)
{
int l,r,c;
scanf("%d%d%d",&l,&r,&c);
a[i].index = l;
a[i].mark = 0;
a[i].value = r - l + 1;
a[i].cost = c;
a[i+1].index = r;
a[i+1].mark = 1;
a[i+1].value = r - l + 1;
a[i+1].cost = c;
}
map <int,int> mp;
sort(a,a+2*n,cmp);
int ans = INT_MAX;
for(int i = 0; i < 2*n; i++)
{
if(a[i].mark)
{
int v = a[i].value,c = a[i].cost;
mp[v] = mp[v] == 0 ? c : min(mp[v],c);
}
else
{
int v = x - a[i].value;
if(v >= 0 && mp[v] != 0) ans = min(ans,a[i].cost + mp[v]);
}
}
ans = ans == INT_MAX ? -1 : ans;
printf("%d\n",ans);
}