Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!(扫描线)

来源:互联网 发布:炒股软件二次开发 编辑:程序博客网 时间:2024/06/05 15: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 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 liricosti — 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!

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 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) = 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个线段,要求找到两个不相交的线段,使得线段长度和正好为m,输出最小花费
解题思路:最近总是遇到一些线段问题,这道题主要做法是将头尾岔开来处理,遍历即可
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>using namespace std;typedef long long LL;struct point{LL cost;int pos,len,flag;}a[400005];LL l[300005];LL cnt;int i,n,x,len;bool cmp(point x,point y){if(x.pos==y.pos)return x.flag<y.flag;return x.pos<y.pos;}int main(){cin>>n>>x;for(i=1;i<=n;i++){cin>>a[i].pos>>a[i+n].pos>>a[i].cost;a[i+n].cost=a[i].cost;a[i].flag=0;a[i+n].flag=1;a[i].len=a[i+n].len=a[i+n].pos-a[i].pos+1;}cnt=2e9+10;sort(a+1,a+1+2*n,cmp);for(i=1;i<=2*n;i++){if(a[i].flag==0){len=x-a[i].len;if(len<=0) continue;if(l[len]==0) continue;cnt=min(cnt,l[len]+a[i].cost);}else{len=a[i].len;if(l[len]==0) l[len]=a[i].cost;else l[len]=min(l[len],a[i].cost);}}if(cnt==2e9+10)cout<<-1<<endl;elsecout<<cnt<<endl;}



阅读全文
0 0
原创粉丝点击