C. Hacker, pack your bags!(Round422)
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, ri,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!
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 li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
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.
4 5
1 3 4
1 2 5
5 6 1
1 2 4
5
3 2
4 6 3
2 4 1
3 5 4
-1
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.
hint:给你n个区间,每个区间的长度为r-l+1,每个区间有一个权值,在恰好满足两个不想交区间的长度等于给定长度的情况下求最小权值和,如果找不到满足条件的区间则输出-1
分别按区间的起点升序排序,区间的终点升序排序,然后固定起点,在终点数组中找满足不想交的区间,然后更新距离数组(初始化为无穷大),然后判断是否有满足条件的距离数组加上当前这个起点区间的权更小,更小则更新。(为什么大家都能想到这个方法。。。很好想吗。。。不觉得啊)
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<limits.h> //使用函数INT_MAX 新技能get 5 using namespace std; 6 typedef long long ll; 7 const int maxn = 2e5+10; 8 struct node{ 9 int l, r, cost; 10 }a[maxn], b[maxn]; 11 ll mincost[maxn]; 12 bool cmp1(node a, node b){ 13 return a.l < b.l; 14 } 15 bool cmp2(node a, node b){ 16 return a.r < b.r; 17 } 18 ll cmp3(ll a, ll b){ 19 return a>b ? b:a; 20 } 21 22 23 int main(){ 24 int n, x; 25 scanf("%d %d", &n, &x); 26 for(int i=0; i<maxn; i++) mincost[i]=INT_MAX; 27 for(int i=0; i<n; i++){ 28 int l, r, cost; 29 scanf("%d %d %d", &l, &r, &cost); 30 a[i].l = b[i].l = l; 31 a[i].r = b[i].r = r; 32 a[i].cost = b[i].cost = cost; 33 } 34 sort(a, a+n, cmp1); 35 sort(b, b+n, cmp2); 36 int MinCost = INT_MAX; 37 int j=0; 38 for(int i=0; i<n; i++){ 39 //while这里不能忘了增量的下标增加 40 while(j<n && b[j].r<a[i].l){ 41 mincost[b[j].r-b[j].l+1] = cmp3(mincost[b[j].r-b[j].l+1], b[j].cost); 42 j++; 43 } 44 int k = x-(a[i].r-a[i].l+1); 45 if(k>0 && (mincost[k]+a[i].cost)<MinCost) MinCost = mincost[k]+a[i].cost; 46 } 47 if(MinCost == INT_MAX) printf("-1\n"); 48 else printf("%d\n", MinCost); 49 50 return 0; 51 }