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


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 li,ricosti — 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 orrj < 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个区间,区间的价值为r-l+1,花费cost,求任意两个不相交的区间价值和为x的最小花费是多少。

思路:这题很有意思,看了别人的思路发现用到了没用过的技巧,把区间拆成两个点起点和终点,然后按点从小到大排序,每次扫到的点如果是右端点则更新该区间价值的最小花费,即如果该区间的价值为value,则dp[value]=min(dp[value],cost),如果是左端点则用该区间的花费和前面已知的最小花费求出当前花费来更新答案。具体操作看代码:

#include<iostream>  
#include<cmath>  
#include<queue>  
#include<cstdio>  
#include<queue>  
#include<algorithm>  
#include<cstring>  
#include<string>  
#include<utility>
#include<map>
#include<stack>
#include<vector>
#define maxn 200005
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const double eps = 1e-8;
const int mod = 1e9 + 7;
struct node{
	int index, mark, value, cost;
}p[maxn << 1];
bool cmp(node a, node b){
	return a.index < b.index || (a.index == b.index&&a.mark < b.mark);
}
int dp[maxn];
int main(){
	int n, x;
	scanf("%d%d", &n, &x);
	int number = 0;
	while (n--){
		int l, r, cost;
		scanf("%d%d%d", &l, &r, &cost);
		p[number].index = l;
		p[number + 1].index = r;
		p[number].mark = 0;
		p[number + 1].mark = 1;
		p[number].value = p[number + 1].value = r - l + 1;
		p[number].cost = p[number + 1].cost = cost;
		number += 2;
	}
	sort(p, p + number, cmp);
	memset(dp, inf, sizeof(dp));
	int ans = inf << 1;
	for (int i = 0; i < number; i++){
		if (p[i].mark)
			dp[p[i].value] = min(dp[p[i].value], p[i].cost);
		else{
			int value = x - p[i].value;
			if (value >= 0 && dp[value] != inf)
				ans = min(ans, dp[value] + p[i].cost);
		}
	}
	ans = ans == inf << 1 ? -1 : ans;
	printf("%d\n", ans);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值