C. 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!
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 duration exactly 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
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.
#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;
}