822C Hacker, pack your bags!
来源:互联网 发布:淘宝宝贝图片素材 编辑:程序博客网 时间:2024/06/07 06:02
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 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 orrj < li.
Help Leha to choose the necessary vouchers!
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.
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.
4 51 3 41 2 55 6 11 2 4
5
3 24 6 32 4 13 5 4
-1
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.
import java.util.*;public class Main{public static void main(String args[]){ Scanner reader=new Scanner(System.in); int a=reader.nextInt(); int b=reader.nextInt(); L x[]=new L[200005]; L y[]=new L[200005]; int z[]=new int[200005]; for(int n=0;n<200005;n++){ z[n]=2000000005; x[n]=new L(); y[n]=new L(); } for(int n=0;n<a;n++){ int st=reader.nextInt(); int en=reader.nextInt(); int co=reader.nextInt(); A xx=new A(st,co,en-st+1); A xx1=new A(en,co,en-st+1); x[st].x.add(xx); y[en].x.add(xx1); } int min=2000000005; for(int n=1;n<=200000;n++){ //System.out.println(x[n].x.size()); for(int m=0;m<x[n].x.size();m++){ if(x[n].x.get(m).t<b&&z[b-x[n].x.get(m).t]!=2000000005){ min=Math.min(min,z[b-x[n].x.get(m).t]+x[n].x.get(m).co); } } for(int m=0;m<y[n].x.size();m++){ int t1=y[n].x.get(m).t; z[t1]=Math.min(z[t1], y[n].x.get(m).co); } } if(min==2000000005){ System.out.println(-1); } else{ System.out.println(min); } }}class L{ArrayList<A> x;L(){x=new ArrayList<A>();}}class A{int st;int t;int co;A(int a,int b,int c){st=a;co=b;t=c;}}
- codeforces 822 C Hacker, pack your bags!
- 822C Hacker, pack your bags!
- CodeForces 822C Hacker, pack your bags!
- 822C Hacker, pack your bags!
- Hacker, pack your bags!
- CodeFroces 822C.Hacker, pack your bags!(sorting+线性枚举)
- Codeforces 822C Hacker, pack your bags!(思维+技巧)
- Codeforces 822C Hacker, pack your bags!【排序+二分】
- codeforces 822 C. Hacker, pack your bags!(思维+dp)
- Codeforces 822 C. Hacker, pack your bags! 思维
- 【Codeforces 822 C. Hacker, pack your bags!】+ pair
- Codeforces 822C Hacker pack your bags! 区间
- cf C. Hacker, pack your bags!
- CF422 div2 C. Hacker, pack your bags!
- Codeforces822 C. Hacker, pack your bags!
- Round 422 C. Hacker, pack your bags!
- codeforces 882C Hacker, pack your bags!
- codeforces Hacker, pack your bags!
- ARM裸机开发入门(一)
- 数据结构-->二叉树
- jzoj 3450_山峰_bfs
- NetMQ学习一
- day_01至day_02
- 822C Hacker, pack your bags!
- 剑指offer——机器人的运动范围(还行)
- Android ListView实现滑动删除
- Android开发之sqlite
- 插入排序(INSERTION-SORT)
- BZOJ 2152:聪聪可可
- Django-ORM多对多查询 (自定义第三张表&ManyToManyField)
- JZOJ__Day 2:【NOIP普及模拟】分数
- 5-3 奥运排行榜 (25分)