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
input
3 2
4 6 3
2 4 1
3 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个区间,每个区间有个费用,选择两个不相交(端点也不能重叠)的区间使得它们的长度总和恰好为x,并且使得它们的费用和最小。如果无解输出-1。

  根据常用套路,按照区间的长度进行分组。然后按照端点的大小(反正按左端点还是右端点都是一样的)进行排序。再拿两个数组跑一道前缀最小值和后缀最小值。

  现在枚举每一条旅行方案,在和它的长度之和恰好为x的另一个数组中,可以用lower_bound和upper_bound快速找出两侧合法的的最后一个和第一个,然后更新一下就好了(详细可以看代码)。

Code

 /**
* Codeforces
* Problem#822C
* Accepted
* Time:124ms
* Memory:16416k
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
const double eps = 1e-;
const int binary_limit = ;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} typedef class Trip {
public:
int l;
int r;
int fee; Trip(int l = , int r = , int fee = ):l(l), r(r), fee(fee) { } boolean operator < (Trip b) const {
return l < b.l;
}
}Trip; boolean operator < (const int& b, const Trip& a) {
return b < a.l;
} int n, x;
vector<Trip>* a;
vector<int>* minu; // in order
vector<int>* mind; // in opposite order inline void init() {
readInteger(n);
readInteger(x);
a = new vector<Trip>[x];
minu = new vector<int>[x];
mind = new vector<int>[x];
for(int i = , l, r, c; i <= n; i++) {
readInteger(l);
readInteger(r);
readInteger(c);
int len = r - l + ;
if(len >= x) continue;
a[len].push_back(Trip(l, r, c));
}
} int res = inf;
inline void solve() {
for(int i = ; i < x; i++)
if(!a[i].empty()) {
sort(a[i].begin(), a[i].end());
minu[i].resize(a[i].size());
mind[i].resize(a[i].size());
for(int j = ; j < (signed)a[i].size(); j++) {
if(j == ) minu[i][j] = a[i][j].fee;
else minu[i][j] = min(a[i][j].fee, minu[i][j - ]);
}
for(int j = (signed)a[i].size() - ; j >= ; j--) {
if(j == a[i].size() - ) mind[i][j] = a[i][j].fee;
else mind[i][j] = min(a[i][j].fee, mind[i][j + ]);
}
} for(int i = ; i < x; i++) {
// cout << i << ":" << endl;
if(!a[i].empty()) {
for(int j = ; j < a[i].size(); j++) {
// cout << a[i][j].l << " " << a[i][j].r << endl;
Trip &t = a[i][j];
int len = t.r - t.l + ;
int ulen = x - len;
int pos = upper_bound(a[ulen].begin(), a[ulen].end(), t.r) - a[ulen].begin();
if(pos != a[ulen].size()) smin(res, t.fee + mind[ulen][pos]);
pos = lower_bound(a[ulen].begin(), a[ulen].end(), t.l - ulen + ) - a[ulen].begin() - ;
if(pos != -) smin(res, t.fee + minu[ulen][pos]);
}
}
} if(res == inf)
puts("-1");
else
printf("%d\n", res);
} int main() {
init();
solve();
return ;
}

Codeforces 822C Hacker, pack your bags! - 贪心的更多相关文章

  1. CodeForces 754D Fedor and coupons&&CodeForces 822C Hacker, pack your bags!

    D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  2. Codeforces 822C Hacker, pack your bags!(思维)

    题目大意:给你n个旅券,上面有开始时间l,结束时间r,和花费cost,要求选择两张时间不相交的旅券时间长度相加为x,且要求花费最少. 解题思路:看了大佬的才会写!其实和之前Codeforces 776 ...

  3. CodeForces 822C Hacker, pack your bags!

    题意 给出一些闭区间(始末+代价),选取两段不重合区间使长度之和恰为x且代价最低 思路 相同持续时间的放在一个vector中,内部再对起始时间排序,从后向前扫获取对应起始时间的最优代价,存在minn中 ...

  4. Codefroces 822C Hacker, pack your bags!

    C. Hacker, pack your bags! time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  5. Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心

    C. Hacker, pack your bags!     It's well known that the best way to distract from something is to do ...

  6. CF822C Hacker, pack your bags!(思维)

    Hacker, pack your bags [题目链接]Hacker, pack your bags &题意: 有n条线段(n<=2e5) 每条线段有左端点li,右端点ri,价值cos ...

  7. Codeforces822 C. Hacker, pack your bags!

    C. Hacker, pack your bags! time limit per test 2 seconds memory limit per test 256 megabytes input s ...

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

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

  9. codeforces 822 C. Hacker, pack your bags!(思维+dp)

    题目链接:http://codeforces.com/contest/822/submission/28248100 题解:多维的可以先降一下维度sort一下可以而且这种区间类型的可以拆一下区间只要加 ...

随机推荐

  1. 胸片和CT断层图像是怎么来的?

    本文作者系医科大学青年教师,关注公众号"计算机视觉life"菜单栏回复"医学" 进群交流 如何得到CT断层图像? 相信小伙伴体检的时候都拍过胸片,假如哪个不幸的 ...

  2. cocos2d-js 遮挡层(禁止触摸事件传递层)

    在游戏中,我们经常会碰到一些弹窗,这些弹窗禁止点透,也就是禁止触摸事件传递到底层,我们称之为遮挡层,这些遮挡层,需要开发遮挡层,我们首先得了解cocos2d-js的触摸传递机制,本文主要针对cocos ...

  3. 题外话:Lua脚本语言存在的意义

    纯属个人见解. 大致来说:c/c++执行效率高,游戏中一些性能敏感的复杂计算需要用c/c++来实现,防止游戏卡顿和低帧率.这些复杂计算包括战斗逻辑,复杂AI,骨骼动画蒙皮骨骼点的坐标计算等等.但c++ ...

  4. 合并dict、list的方法

    dict1={1:[1,11,111],2:[2,22,222]}dict2={3:[3,33,333],4:[4,44,444]}合并两个字典得到类似 {1:[1,11,111],2:[2,22,2 ...

  5. C# 调用.bat 提示该命令不是内部命令或外部命令

    前提:双击.bat文件可以执行成功,用C#调用提示该命令不是内部命令或外部命令...... 解决方法:下面代码的红色标注,既要设置.bat文件的文件名FileName,也要设置.bat文件所在的文件夹 ...

  6. Codeforces Round #319 (Div. 2) D

    E A tree of size n is an undirected connected graph consisting of n vertices without cycles. Conside ...

  7. Subway (树中心 + 树hash)

    首先找到树的中心或者中心,我这里是找中心,因为我们需要找一个相同的起点,然后最多2个中心就是树的宽度为偶数时,奇数时为1个. 找到之后需要对树进行hash,使得每个点都具备独特性,使之树的形态能够保证 ...

  8. Python 连接SQL Server数据库 - pymssql使用基础

    1.  官方api http://www.pymssql.org/en/stable/ref/pymssql.html 我学习自这里

  9. Hive分区表新增字段及修改表名,列名,列注释,表注释,增加列,调整列顺序,属性名等操作

    一.Hive分区表新增字段 参考博客:https://blog.csdn.net/yeweiouyang/article/details/44851459 二.Hive修改表名,列名,列注释,表注释, ...

  10. Python词云分析

    import jieba from matplotlib import pyplot as plt from wordcloud import WordCloud from PIL import Im ...