Codeforces 822C Hacker, pack your bags (vector+upper_bound,或思维)

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 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 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 li, ri 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) = 5 and 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个旅券,上面有开始时间l,结束时间r,和花费cost,要求选择两张时间不相交的旅券时间长度相加为x,且要求花费最少。
思路: 这种题蛮经典的,还是跟线段有关- -,他要求不相交, 而且相加等于x,其实很简单,把所有端点从小到大排序, 如果这个点是左端点, 那么他可以加上前面已经走过的完整的区间中的差等于 x - 当前区间差 中 cost最小的值, 怎样记录前面走完区间的cost呢。。 只要碰到右端点, 就把他这个区间的差更新最小的。。用一个sum数组记录,这样只要sum数组有值,说明前面完整的区间的差有值。。跟http://blog.csdn.net/qq_34374664/article/details/72377345这题有点像, 然后用了一个数组记录出现过得值得方法也挺经典。。这里有个小trick, 这里排序的时候,让左端点在前面, 因为不想交要求端点也不一样,端点值相等时,左端点在前面,不会把前面区间的右端点算进去。。因为这不是个完整的区间。。
 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>

using namespace std;
int n,x;

struct nodel
{
    nodel(int ll,int rr,long long cc){
        l=ll;r=rr;c=cc;
    }
    int l,r;
    long long c;
    long long minn;
    friend bool operator<(nodel a,nodel b)
    {
        return a.l<b.l;
    }
};
struct noder
{
    noder(int ll,int rr,long long cc){
        l=ll;r=rr;c=cc;
    }
    int l,r;
    long long c;
    long long minn;
    friend bool operator<(noder a,noder b)
    {
        return a.r<b.r;
    }
};
vector<nodel> itl[200005];
vector<noder> itr[200005];

int main()
{
    while(~scanf("%d%d",&n,&x))
    {
        for(int i=0;i<=200000;i++)
            itl[i].clear(),itr[i].clear();

        for(int i=0;i<n;i++)
        {
            int l,r;
            long long c;
            scanf("%d%d%lld",&l,&r,&c);
            nodel curl=nodel(l,r,c);
            noder curr=noder(l,r,c);

            int tp=(r-l+1);
            itl[tp].push_back(curl);
            itr[tp].push_back(curr);
        }
        for(int i=0;i<=200000;i++)
        {
            if(itl[i].size()!=0){
                sort(itl[i].begin(),itl[i].end());
                int len=itl[i].size();
                long long tmin=1e18;
                for(int j=len-1;j>=0;j--)
                {
                    tmin=min(tmin,itl[i][j].c);
                    itl[i][j].minn=tmin;
                }
            }
            if(itr[i].size()!=0){
                sort(itr[i].begin(),itr[i].end());
                int len=itr[i].size();
                long long tmin=1e18;
                for(int j=0;j<len;j++)
                {
                    tmin=min(tmin,itr[i][j].c);
                    itr[i][j].minn=tmin;
                }
            }
        }
        long long ans=1e18;
        for(int i=1;i<=(x/2);i++)
        {
            int tp=x-i;
            int len=itl[i].size();
            for(int j=0;j<len;j++)
            {
                long long tans=1e18;
                int tl=itl[i][j].l;
                int id=lower_bound(itr[tp].begin(),itr[tp].end(),noder(0,tl,0))-itr[tp].begin();
                id--;
                if(id>=0 && id<itr[tp].size())
                {
                    tans=min(tans,itl[i][j].c+itr[tp][id].minn);
                }

                int tr=itl[i][j].r;
                id=upper_bound(itl[tp].begin(),itl[tp].end(),nodel(tr,0,0))-itl[tp].begin();
                if(id>=0 && id<itl[tp].size())
                {
                    tans=min(tans,itl[i][j].c+itl[tp][id].minn);
                }
                ans=min(ans,tans);
            }
        }
        if(ans!=1e18)
            printf("%lld\n",ans);
        else
            printf("-1\n");
    }
}
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int INF = 2e9 + 5;
struct node
{
    int x, y,cost, type;
    node(){}
    node(int xx, int yy, int cc, int tt): x(xx), y(yy), cost(cc), type(tt){}
};
int cmp(node a, node b)
{
    if(a.x == b.x)
        return a.type < b.type;
    return a.x < b.x;
}
vector<node> v;
int sum[maxn];
int main()
{
    int n, x;
    while(~scanf("%d%d", &n, &x))
    {
        int l, r, c;
        v.clear();
        memset(sum, 0, sizeof(sum));
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d%d", &l, &r, &c);
            v.push_back(node(l, r, c, -1));
            v.push_back(node(r, l, c, 1));
        }
        sort(v.begin(), v.end(), cmp);
        int ans = INF;
        for(int i = 0; i < v.size(); i++)
        {
            if(v[i].type == -1)
            {
                int temp = v[i].y-v[i].x+1;
                if(temp >= x || !sum[x-temp]) continue;
                ans = min(ans, sum[x-temp]+v[i].cost);
            }
            else
            {
                int temp = v[i].x-v[i].y+1;
                if(!sum[temp] || sum[temp] > v[i].cost)
                    sum[temp] = v[i].cost;
            }
        }
        if(ans != INF)
            printf("%d\n", ans);
        else
            printf("-1\n");
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值