zoukankan      html  css  js  c++  java
  • 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 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
    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.

     dp[i]代表旅游i天的最小花费。按照起始时间,结束时间分别从小到大排序,从后向前递推。

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<iostream>
    #include<queue>
    #include<map>
    #include<cmath>
    #include<set>
    #include<stack>
    #define ll long long
    #define pb push_back
    #define max(x,y) ((x)>(y)?(x):(y))
    #define min(x,y) ((x)>(y)?(y):(x))
    #define cls(name,x) memset(name,x,sizeof(name))
    using namespace std;
    const int inf=1e9+10;
    const ll llinf=1e16+10;
    const int maxn=2e5+10;
    const int maxm=20;
    const int mod=1e9+7;
    const double pi=acos(-1.0);
    int n,x;
    struct node
    {
        int l,r;
        ll cost;
    }savel[maxn],saver[maxn];
    bool cmpr(const node &a,const node &b)
    {
        return a.r<b.r;
    }
    bool cmpl(const node &a,const node &b)
    {
        return a.l<b.l;
    }
    ll dp[maxn];
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(~scanf("%d %d",&n,&x))
        {
            ll ans=llinf;
            for(int i=0;i<n;i++)
            {
                int l,r;
                scanf("%d %d %I64d",&savel[i].l,&savel[i].r,&savel[i].cost);
                saver[i]=savel[i];
            }
            sort(savel,savel+n,cmpl);
            sort(saver,saver+n,cmpr);
            for(int i=0;i<maxn;i++)
                dp[i]=llinf;
            int k=n-1;
            for(int i=n-1;i>=0;i--)
            {
                while(savel[k].l>saver[i].r&&k>=0)
                {
                    dp[savel[k].r-savel[k].l+1]=min(dp[savel[k].r-savel[k].l+1],savel[k].cost);
                    k--;
                }
                int t=saver[i].r-saver[i].l+1;
                if(x-t>=1)
                {
                    ans=min(ans,dp[x-t]+saver[i].cost);
                }
            }
            if(ans==llinf)
                printf("-1
    ");
            else
            printf("%I64d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    技术领导要不要写代码?
    资深程序员告诉你:如何用五年时间攒够100万?
    mfc基于对话框的应用程序,如何设置初始对话框大小,移动控件位置
    zend studio,操作记录
    xampp怎么操作数据库mysql
    mysql-font的理解
    delphi 中配置文件的使用(*.ini)和TIniFile 用法
    delphi 创建服务,安装与卸载服务
    sublime Text的一些用法(emmet插件、)
    apache (web服务器) ->php->mysql,xampp与wamp比较,WAMP与WNMP有什么区别
  • 原文地址:https://www.cnblogs.com/mgz-/p/7110539.html
Copyright © 2011-2022 走看看