CodeFroces 822C.Hacker, pack your bags!(sorting+线性枚举)

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 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.



哇由于比赛时候我很傻逼的用了vector的erase操作,所以复杂度直接爆炸。。。后来怎么改都T,遂跟随dalao的步伐。

做法是这样的。

1.我们开两个数组voc_l和voc_r,都是记录一开始l  r   cost三个变量,完全一模一样。

2.我们对第一个数组以 l 从小到大排序,第二个数组以 r 从小到大排序

3.我们规定,第一个数组是放在后面的,第二个数组是放在前面的,也就是一一对应的时候第一个数组的l要大于第二个数组的r。

4.我们枚举第一个数组,并且对第二个数组有个标记rc,保证voc_r[rc].r < voc_l[lc].l。此时用一个数组min_cost,其作用为记录长度为i的最小花费是多少。只要这个成立,我们就一直更新第二个数组的标记rc++。直到不成立后,我们知道第一个数组的l r cost,也就是知道x - (r - l + 1),直接在min_cost里面找,更新一次答案。

5.输出答案


分析一下,复杂度为快排的O(nlogn),后面线性遍历才2n复杂度。


代码如下:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int INF = 2e9 + 1;
int n, x;
int min_cost[maxn];

struct node{
	int l, r, cost;
	node(){}
	node(int a, int b, int c){
		l = a;
		r = b;
		cost = c;
	}
};

node voc_l[maxn], voc_r[maxn];

bool cmp1(node a, node b){
	if(a.l == b.l){
		return a.cost < b.cost;
	}
	return a.l < b.l;
}

bool cmp2(node a, node b){
	if(a.r == b.r){
		return a.cost < b.cost;
	}
	return a.r < b.r;
}

int main(){
	int l, r, cost, tmp;
	int Min = INF;
	fill(min_cost, min_cost + maxn, INF); 
	cin >> n >> x;
	for(int i = 0; i < n; i++){
		cin >> l >> r >> cost;
		voc_l[i] = (node(l, r, cost));
		voc_r[i] = (node(l, r, cost));
	}
	
	sort(voc_l, voc_l + n, cmp1);
	sort(voc_r, voc_r + n, cmp2);
	
	int lc = 0, rc = 0;
	for(; lc < n; lc++){
		while(rc < n && voc_r[rc].r < voc_l[lc].l){
			tmp = voc_r[rc].r - voc_r[rc].l + 1;
			if(min_cost[tmp] > voc_r[rc].cost)
				min_cost[tmp] = voc_r[rc].cost;
			rc++; 
		}
		if(min_cost[x - (voc_l[lc].r - voc_l[lc].l + 1)] != INF &&  x - (voc_l[lc].r - voc_l[lc].l + 1) >= 0) 
			Min = min(Min, voc_l[lc].cost + min_cost[x - (voc_l[lc].r - voc_l[lc].l + 1)]);
	}
	
	if(Min == INF)
		cout << "-1" << endl;
	else
		cout << Min << endl;
	return 0;
}


因为打死不用long long所以还要加个判定之类的。注意细节。找得出这些错误也说明自己对题目的数据,题意都十分了解,而且真的明白自己的代码。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值