Codeforces Round #422 (Div. 2) C Hacker, pack your bags! (二分orDP)

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 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 to2.

【题意】:

给定n个区间段,每个区间段都有自己的价值,求任意两段的长度加和等于x,且花费最小的区间段,若不存在,则输出-1.

 

【算法】:

1.      贪心+二分

2.      DP(有能力的可以自己了解一下)

 

【分析】:

可以一层循环枚举出每一段区间的长度,然后第二层循环找到相加等于x的区间长度,再维护一个最小值,时间复杂度O(n^2),超时。

 

优化:每个区间的长度可以轻易的得到,可以枚举每一个区间长度,然后去找另一个符合题意的区间长度,此处可以运用二分查找,时间复杂度O(n*log(n))。

 

【实现】:

每个区间段有3个参数,可以设一个结构体用来保存。

struct node{

         intl,r,c,value,cost;

}p[200005];

 

分别储存区间端点,区间长度,区间价值,前缀最小值。

 

因为要二分,所以要区间有序,首先要确定排序策略:1.按照区间长度由小到大(相反也可以)排序  2.若区间长度相等,按照右端点由小到大排序。因为若满足p[i].r>p[j].l的话,可以得到此时的区间没有交集,最外层的循环是从0到n,每个数都跑一遍,可以看p[i]之后符合题意的区间,最外层循环会枚举到每一种情况(两者一前一后,枚举到后面时,自然会找到前面的)。

 

如何二分?

用x-p[i].c得到要找的值,若是有多个符合条件的值如何处理?

因为不用扫P[i]后符合条件的值,所以只要求的p[i]前符合条件的最小值,排序时按r从小到大排序就在此时得到应用,前面的只需要找到最接近p[i]的区间,维护最小值可以在排序之后,for循环维护。

for(i=0;i<n;i++)

{

         if(i>0&&p[i].c==p[i-1].c)

         p[i].cost=min(p[i].cost,p[i-1].cost);

}

 

注意数据会爆int,所以用long long维护最小值,最小值初始化为一个较大的long long类型的数据即可。

 

 

代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const ll inf=1e18;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
struct node{
	int l,r,c,value,cost;
}p[200005];

int cmp(node a,node b)
{
	if(a.c==b.c)
	return a.r<b.r;
	return a.c<b.c;
}
int main()
{
	int n,x,i,j,k;
	scanf("%d%d",&n,&x);
	for(i=0;i<n;i++)
	{
		scanf("%d%d%d",&p[i].l,&p[i].r,&p[i].value);
		p[i].c=p[i].r-p[i].l+1;
		p[i].cost=p[i].value;
	}
	ll minn=inf;
	sort(p,p+n,cmp);
	for(i=0;i<n;i++)
	{
		if(i>0&&p[i].c==p[i-1].c)
		p[i].cost=min(p[i].cost,p[i-1].cost);
	}
	for(i=0;i<n;i++)
	{
		if(p[i].c>=x)
		continue;
		ll temp=x-p[i].c,v=p[i].value;
		int l=0,r=n-1,mid;
		while(l<=r)
		{
			mid=(l+r)/2;
			
			if(p[mid].c<temp)
			l=mid+1;
			else if(p[mid].c==temp)
			{
				if(p[i].l<=p[mid].r)
				r=mid-1;
				else
				{
					l=mid+1;
					minn=min(minn,v+p[mid].cost);
				}
			}
			else
			r=mid-1;
		}
	}
	if(minn==inf)
	cout<<"-1"<<endl;
	else
	cout<<minn<<endl;
	return 0;
}

DP更容易理解,不再解释。

代码实现请自行百度。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值