Codeforces 822C Hacker, pack your bags!【排序+二分】

来源:互联网 发布:算法工程师的基本要求 编辑:程序博客网 时间:2024/06/05 17:05

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 exactlyx 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 thatn vouchers left. i-th voucher is characterized by three integersli,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 vouchersi and j(i ≠ j) so that they don't intersect, sum of their durations isexactly x and their total cost is as minimal as possible. Two vouchersi and j don't intersect if only at least one of the following conditions is fulfilled:ri < lj orrj < li.

Help Leha to choose the necessary vouchers!

Input

The first line contains two integers n andx (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 integersli,ri andcosti(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 durationexactly x.

Examples
Input
4 51 3 41 2 55 6 11 2 4
Output
5
Input
3 24 6 32 4 13 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 be4 + 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 to2.


题目大意:


现在一共有N个旅游表,时间是从Li到Ri花费为Ci.

现在主人公想要选取两个旅游项目,使得总共的旅游天数恰好为x天,并且希望花费最小。

两个旅游项目不能重叠。


思路:


我们用vector<node >a[i]存入旅游天数为i天的所有旅游信息。

那么对于每个a【i】对其进行时间排序,以开始时间从小到大排序。

然后我们枚举出来任意一个旅游项目,假设此旅游项目为k天,那么对应我们在x-k天中的数组中二分一个位子,使得这个位子之后的所有旅游项目都可以进行选择(不重叠了)

那么在这里我们维护一个后缀最小值即可。


过程模拟一下,不难。


Ac代码:

#include<stdio.h>#include<string.h>#include<algorithm>#include<map>#include<vector>using namespace std;struct node{    int x,y,w;}a[350000];int ans[350000];vector<node>mp[350000];vector<int>minn[350000];int cmp(node a,node b){    return a.x<b.x;}int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        for(int i=1;i<=200000;i++)mp[i].clear(),minn[i].clear();        for(int i=0;i<n;i++)scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);        sort(a,a+n,cmp);        for(int i=0;i<n;i++)        {            mp[a[i].y-a[i].x+1].push_back(a[i]);        }        for(int i=1;i<=200000;i++)        {            for(int j=mp[i].size()-1;j>=0;j--)            {                if(j==mp[i].size()-1)ans[j]=mp[i][j].w;                else ans[j]=min(mp[i][j].w,ans[j+1]);            }            for(int j=0;j<mp[i].size();j++)            {                minn[i].push_back(ans[j]);            }        }        int output=2e9+7;        for(int i=1;i<=200000;i++)        {            if(m-i<0)break;            for(int j=0;j<mp[i].size();j++)            {                int pos=-1;                int l=0;                int r=mp[m-i].size()-1;                while(r-l>=0)                {                    int mid=(l+r)/2;                    if(mp[m-i][mid].x>mp[i][j].y)                    {                        pos=mid;                        r=mid-1;                    }                    else l=mid+1;                }                if(pos==-1)continue;                else output=min(output,mp[i][j].w+minn[m-i][pos]);            }        }        if(output==2e9+7)printf("-1\n");        else printf("%d\n",output);    }}








原创粉丝点击