codeforces #422 C. Hacker, pack your bags!(贪心dp)

来源:互联网 发布:amarra for mac 编辑:程序博客网 时间:2024/05/22 20:16

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,ricosti — 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 orrj < 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 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) = 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 to2.

【题意】:

给出n个区间段,每个区间有一个价值,现在要找到两个不相交的区间段,使得长度和为x,且价值最小,输出最小价值。

注意:定义区间长度为r-l+1,且端点重合也属于相交


【分析】:

方法1:

有点像贪心+dp维护。

结构体变量存点的信息,每个点包括 坐标p,所属区间长度len,所属区间价值val,标志位(左端点or右端点)


按坐标从小到大扫描。

遇到右端点时,更新数组dp【len】表示单个区间长度为len时的最小价值。

遇到左端点时,取dp【x-len】加上当前区间的价值,更新答案ans的最小值。


方法2:

枚举区间+二分查找

按区间长度排序,枚举区间,用二分查找互补的区间,价值相加,用ans记录最小价值


【代码-方法1】:

#include<bits/stdc++.h>using namespace std;typedef long long ll;const ll INF=0x3f3f3f3f3f3f;struct node{    int p,len;//坐标和长度    ll val; //价值    bool flag;//标记左右    node(int P=0,int L=0,ll V=0,bool F=0){        p=P;len=L;val=V;flag=F;    }}e[505050];ll dp[502020];bool cmp(node a,node b){    if(a.p==b.p)return a.flag<b.flag;    return a.p<b.p;}int main(){    ios::sync_with_stdio(0);    int n,x,u,v,w;    while(cin>>n>>x)    {        for(int i=0;i<n;i++)        {            cin>>u>>v>>w;            e[i]=node(u,v-u+1,w,0);            e[i+n]=node(v,v-u+1,w,1);        }        sort(e,e+n*2,cmp);        for(int i=0;i<200000;i++)dp[i]=INF;        ll ans=INF;        for(int i=0;i<2*n;i++)        {            if(e[i].flag==0&&x-e[i].len>=0)//left                ans=min(ans,e[i].val+dp[x-e[i].len]);            else if(e[i].flag) //right                dp[e[i].len]=min(dp[e[i].len],e[i].val);        }        if(ans==INF)ans=-1;        cout<<ans<<endl;    }}

【代码-方法2】:不想写了..

阅读全文
1 0