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
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 to 2.
题意:给出n个旅券,标定出发时间l,及回归时间r,每张旅券花费为cost,求一个最小花费,使得一共出行x天,且只能选两张旅券。
建立两个数组v1,v2,v1按照出发时间升序排列,v2按照回归时间升序排列;
枚举每个v1,在v2中找寻与v1可以搭配的(暂时仅看出发和回归时间合适,忽略总天数为x),然后维护一个外出n天的最小花费数组minn;
这样枚举过后可以保证选择的两张券外出时间没有交集,同时找出外出x天的最小花费(v1.cost + minn[x-v1.d])
#include<bits/stdc++.h>
using namespace std;
const int M = 2e5 + 5;
struct node
{
int l,r,d,cost;
}v1[M],v2[M];
int minn[M];//记录外出n天的最小花费
bool cmp1(node a, node b)
{
return a.l < b.l;
}
bool cmp2(node a, node b)
{
return a.r < b.r;
}
int main()
{
int n,x;
scanf("%d%d",&n,&x);
for(int i=1;i<=x;i++)
minn[i] = INT_MAX;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d", &v1[i].l, &v1[i].r, &v1[i].cost);
v1[i].d = v1[i].r - v1[i].l + 1;
v2[i] = v1[i];
}
sort(v1+1, v1+n+1, cmp1);
sort(v2+1, v2+n+1, cmp2);
int ans = INT_MAX;
int tmp;
int j = 1;
for(int i=1;i<=n;i++)//假定先确定v1[i]为第一次选取的旅券,找一个符合条件的v2[j]
{
while(j<=n&&v1[i].l>v2[j].r)
{
minn[v2[j].d] = min(minn[v2[j].d], v2[j].cost);//维护minn,始终为外出n天的最小值
j++;
}
tmp = x - v1[i].d;
if(tmp>0&&minn[tmp]!=INT_MAX)
ans = min(ans, minn[tmp]+v1[i].cost);
}
if(ans==INT_MAX)
printf("-1");
else
printf("%d", ans);
return 0;
}