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 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+5, M = 1e3+20,inf = 2e9+10;


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 = 0,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 = 1; i <= X; ++i) {
        mp[i] = INF;
    }
    for(int i = 1; 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 + 1,1,v);
        a[++cnt] = ss(y,y - x + 1,-1,v);
    }
    sort(a+1,a+cnt+1,cmp);
    LL ans = INF;
    for(int i = 1; i <= cnt; ++i) {
        if(a[i].id == 1) {

           // cout<<X-a[i].len<<endl;
            if(X - a[i].len > 0) 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 0;
}
原文地址:https://www.cnblogs.com/zxhl/p/7134024.html