• codeforces 822c-Hacker, pack your bags!


    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.

    题意:给出n个价值cost的线段(区间),选取两条不相交的线段使其长度和为x,并且价值和要尽量小, 输出最小价值和。

    思路:我们可以枚举区间,对第i个区间,更新区间长度为ri-li+1的最小cost, 并且对于1~i的区间,如果区间长度为n-r+l-1的最小cost存在,那么就可以更新1~i的区间得到的结果(即最小价值和)。

    若要实现它,我们可以先以左端点对区间排序,然后遍历端点,左端点更新答案(因为到了左端点,对应的区间与之前的区间没有相交,可以更新答案),右端点更新区间长度为ri-li+1的最小cost。这里由于左右端点都需要知道区间长度,所以每段区间处理成了两段相同区间,代码看上去更优美。

    AC代码:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    const int MAXN=2e5+5;
    struct P{
        int l,r,cost,type;
        P(){}
        P(int ll, int rr, int cc, int tt): l(ll), r(rr), cost(cc), type(tt){}
    };
    vector<P> vec;
    bool cmp(P a, P b)
    {
        if(a.l==b.l)
            return a.type<b.type;
        return a.l<b.l;
    }
    int minn[MAXN];
    int main()
    {
        int n,x;
        int a,b,c;
        scanf("%d %d", &n, &x);
        for(int i=0;i<n;i++){
            scanf("%d %d %d", &a, &b, &c);
            vec.push_back(P(a, b, c, -1));
            vec.push_back(P(b, a, c, 1));
        }
        memset(minn, 0, sizeof(minn));
        
        int res=2e9+5;
        int tmp;
        sort(vec.begin(), vec.end(), cmp);
        for(int i=0;i<vec.size();i++){
            if(vec[i].type==-1)
            {
                tmp=vec[i].r-vec[i].l+1;
                if(tmp>=x||!minn[x-tmp])
                    continue;
                res=min(res, minn[x-tmp]+vec[i].cost);
            }
            else
            {
                tmp=vec[i].l-vec[i].r+1;
                if(!minn[tmp]||vec[i].cost<minn[tmp]){
                    minn[tmp]=vec[i].cost;
                }
            }
            
        }
        if(res==2e9+5)
            cout<<-1<<endl;
        else
            cout<<res<<endl;
        return 0;
    }
  • 相关阅读:
    放弃模拟器,安卓手机WiFi投屏到Ubuntu
    在Ubuntu中使用多显示器远程window主机
    完全卸载visual studio及其组件
    使用defineProperty实现自定义setter, 简化前端Angular的重构工作
    数字图像基础
    Python的文本和字节序列
    机器学习开发流程基础
    深度学习之新闻多分类问题
    深度学习之电影二分类的情感问题
    Chrome89针对sessionStorage的更新导致数据共享问题
  • 原文地址:https://www.cnblogs.com/MasterSpark/p/7413959.html
Copyright © 2020-2023  润新知