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 liri,costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of thei-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 5
1 3 4
1 2 5
5 6 1
1 2 4
output
5
 
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个带权值的线段,一个X

  要你选出两个不想交的线段,满足线段长度和为X,且最小的权值和,不能找到-1

题解:

  取出所有端点

  左端点1,右端点-1,每次遇到左端点,查询小于当前位置的已经出现 的右端点权值,并且满足线段长度和X,排序好后跑一遍就行了

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e16+1LL;
const double pi = acos(-1.0);
const int N = 1e6+, M = 1e3+,inf = 2e9+; struct ss{
int x,len,id,value;
ss(){}
ss(int xs,int lens,int ids,int values){x = xs,len = lens,id = ids,value = values;}
}a[N];
int cnt = ,n,X;
int cmp(ss s1,ss s2) {
if(s1.x == s2.x) return s1.id > s2.id;
return s1.x < s2.x;
}
LL mp[N];
int main() {
scanf("%d%d",&n,&X);
for(int i = ; i <= X; ++i) {
mp[i] = INF;
}
for(int i = ; i <= n; ++i) {
int x,y,v;
scanf("%d%d%d",&x,&y,&v);
// cout<<y-x+1<<endl;
a[++cnt] = ss(x,y - x + ,,v);
a[++cnt] = ss(y,y - x + ,-,v);
}
sort(a+,a+cnt+,cmp);
LL ans = INF;
for(int i = ; i <= cnt; ++i) {
if(a[i].id == ) { // cout<<X-a[i].len<<endl;
if(X - a[i].len > ) ans = min(ans,mp[X - a[i].len] + a[i].value); }
else {
// cout<<a[i].len<<endl;
mp[a[i].len] = min(mp[a[i].len],1LL*a[i].value);//cout<<"fuck"<<endl;
}
}
if(ans == INF) puts("-1");
else
cout<<ans<<endl;
return ;
}

Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心的更多相关文章

  1. Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!(更新数组)

    传送门 题意 给出n个区间[l,r]及花费\(cost_i\),找两个区间满足 1.区间和为指定值x 2.花费最小 分析 先用vector记录(l,r,cost)和(r,l,cost),按l排序,再设 ...

  2. Codeforces Round #422 (Div. 2)

    Codeforces Round #422 (Div. 2) Table of Contents Codeforces Round #422 (Div. 2)Problem A. I'm bored ...

  3. 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(二分写法)

    [题目链接]:http://codeforces.com/contest/822/problem/C [题意] 有n个旅行计划, 每个旅行计划以开始日期li,结束日期ri,以及花费金钱costi描述; ...

  4. 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(hash写法)

    接上一篇文章; 这里直接把左端点和右端点映射到vector数组上; 映射一个open和close数组; 枚举1..2e5 如果open[i]内有安排; 则用那个安排和dp数组来更新答案; 更新答案完之 ...

  5. Codeforces Round #422 (Div. 2) E. Liar 后缀数组+RMQ+DP

    E. Liar     The first semester ended. You know, after the end of the first semester the holidays beg ...

  6. Codeforces Round #422 (Div. 2) B. Crossword solving 枚举

    B. Crossword solving     Erelong Leha was bored by calculating of the greatest common divisor of two ...

  7. Codeforces Round #422 (Div. 2) A. I'm bored with life 暴力

    A. I'm bored with life     Holidays have finished. Thanks to the help of the hacker Leha, Noora mana ...

  8. 【Codeforces Round #422 (Div. 2) D】My pretty girl Noora

    [题目链接]:http://codeforces.com/contest/822/problem/D [题意] 有n个人参加选美比赛; 要求把这n个人分成若干个相同大小的组; 每个组内的人数是相同的; ...

  9. 【Codeforces Round #422 (Div. 2) B】Crossword solving

    [题目链接]:http://codeforces.com/contest/822/problem/B [题意] 让你用s去匹配t,问你最少需要修改s中的多少个字符; 才能在t中匹配到s; [题解] O ...

随机推荐

  1. PTA 08-图9 关键活动 (30分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/719 假定一个工程项目由一组子任务构成,子任务之间有的可以并行执行,有的必须在完成了其它 ...

  2. 【php wamp的配置】

  3. 使用 ftrace 调试 Linux 内核,第 2 部分

    ftrace 操作概述 使用 ftrace 提供的跟踪器来调试或者分析内核时需要如下操作: 切换到目录 /sys/kernel/debug/tracing/ 下 查看 available_tracer ...

  4. CSV文件导出2

    public void exportCSVFile( HttpServletResponse response, ResultSet rs,String fileName,String headers ...

  5. java使用jxl,自动导出数据excle,quartz自动发送邮件

    =============JAVA后台代码===================== package com.qgc.service.autoSendMsg.AutoSendMsg import ja ...

  6. 【kotlin】kotlin中List中添加List怎么操作

    如题,List集合添加一个List集合怎么操作 如上,现在有了List<A>,A类中有个字段List<B>, 新创建一个List<B>,想把LIst<A> ...

  7. 【转】构造HTTP请求Header实现“伪造来源IP”

    构造 HTTP请求 Header 实现“伪造来源 IP ” 在阅读本文前,大家要有一个概念,在实现正常的TCP/IP 双方通信情况下,是无法伪造来源 IP 的,也就是说,在 TCP/IP 协议中,可以 ...

  8. win10中显示wpcap.dll丢失的处理方式

    win10中显示wpcap.dll丢失的处理方式 学习了:https://jingyan.baidu.com/article/4f34706e30e673e387b56dd8.html 直接安装Win ...

  9. Android菜单menu控件大全

    下载:http://www.see-source.com/androidwidget/list.html?type=16 Android-NewPopupMenu 使用PopupWindow实现的Po ...

  10. SolidEdge如何为零件指定不同的颜色 给零件着色 给装配体着色

    格式-零件画笔 可以给零件的一个表面,或者一个特征着色   如果要指定不同的色彩,可以在格式-样式-面样式中修改   如果是给装配体着色,则点击任意零件,在"无"的选项卡里面修改颜 ...