Codeforces822 C. Hacker, pack your bags!
2 seconds
256 megabytes
standard input
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 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 5
1 3 4
1 2 5
5 6 1
1 2 4
5
3 2
4 6 3
2 4 1
3 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 to2.
——————————————————————————————————
题目的意思是给出n个区间,选出两个不相交的区间,区间长度和要等于m,求最小花费
思路:按l排序,枚举区间作为选择的后半段,开始一个数组xx保存长度为i的花费最小值,用一个优先队列维护已处理的区间的所达到的xx。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std; #define LL long long
const LL INF = 0x7fffffffffffffff; struct node
{
int l,r;
LL c;
friend bool operator<(node a,node b)
{
return a.r>b.r;
}
} a[200005];
LL xx[200005]; bool cmp(node a,node b)
{
if(a.l!=b.l)
return a.l<b.l;
return a.c<b.c;
} int main()
{
int n,x;
scanf("%d%d",&n,&x);
for(int i=0; i<n; i++)
{
scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].c);
}
sort(a,a+n,cmp);
LL mn=INF;
priority_queue<node>q;
memset(xx,-1,sizeof xx);
for(int i=0; i<n; i++)
{
int ll=a[i].r-a[i].l+1;
if(ll>=x)
continue;
while(!q.empty()&&q.top().r<a[i].l)
{
int xs=q.top().r-q.top().l+1;
if(xx[xs]==-1)
xx[xs]=q.top().c;
xx[xs]=min(xx[xs],q.top().c);
q.pop();
}
q.push(a[i]);
if(xx[x-ll]==-1)
continue;
mn=min(mn,a[i].c+xx[x-ll]); }
printf("%lld\n",mn==INF?-1:mn);
return 0;
}
Codeforces822 C. Hacker, pack your bags!的更多相关文章
- CF822C Hacker, pack your bags!(思维)
Hacker, pack your bags [题目链接]Hacker, pack your bags &题意: 有n条线段(n<=2e5) 每条线段有左端点li,右端点ri,价值cos ...
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心
C. Hacker, pack your bags! It's well known that the best way to distract from something is to do ...
- CodeForces 754D Fedor and coupons&&CodeForces 822C Hacker, pack your bags!
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codefroces 822C Hacker, pack your bags!
C. Hacker, pack your bags! time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces 822C Hacker, pack your bags! - 贪心
It's well known that the best way to distract from something is to do one's favourite thing. Job is ...
- CF-822C Hacker, pack your bags! 思维题
题目大意是给若干线段及其费用,每个线段权值即为其长度.要求找出两个不重合线段,令其权值和等于x且费用最少. 解法: 先分析一下题目,要处理不重合的问题,有重合的线段不能组合,其次这是一个选二问题,当枚 ...
- Codeforces 822C Hacker, pack your bags!(思维)
题目大意:给你n个旅券,上面有开始时间l,结束时间r,和花费cost,要求选择两张时间不相交的旅券时间长度相加为x,且要求花费最少. 解题思路:看了大佬的才会写!其实和之前Codeforces 776 ...
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!(更新数组)
传送门 题意 给出n个区间[l,r]及花费\(cost_i\),找两个区间满足 1.区间和为指定值x 2.花费最小 分析 先用vector记录(l,r,cost)和(r,l,cost),按l排序,再设 ...
- CF822C Hacker, pack your bags!
思路: 对于一个区间[l, r],只需枚举所有满足r' < l并且二者duration之和为x的区间[l', r'],寻找其中二者cost之和最小的即可.于是可以开一个数组a[],a[i]表示所 ...
随机推荐
- c# sharpsvn 客户端开发测试
1:没有工作副本,上传时会报错的. 会提示本地目录不是not a working copy 而此中文名称就是工作副本, 本地要与svn服务器公用的一个文件夹. 所以每次要checkout 然后才能 ...
- idea快捷键及快捷方法(待完善)
一.快捷键 alt+insert 插入get.set.构造函数等 ctrl+shift+alt+g 生成注释文档 ctrl+shift+alt+z 移除注释文档 Ctrl + Alt ...
- 简单DP入门四连发
复习一下一直不太懂的dp. dp博大精深,路还长着呢 第一题;http://acm.hdu.edu.cn/showproblem.php?pid=2084 从下往上就是水题 #include<c ...
- 爬取掌阅app免费电子书数据
主要介绍如何抓取app数据及抓包工具的使用,能看到这相信你已经有爬虫基础了 编不下去了,主要是我懒,直接开干吧! 一.使用环境和工具 windows + python3 + Jsonpath + Ch ...
- Windows MySQL5.7安装和配置
http://www.leixuesong.cn/category/mysql MySQL5.7是MySQL是最新的MySQL大版本,也是官方认为目前性能最好的.MySQL5.7也有很多改动,很多新的 ...
- Luogu 1341 无序字母对 - 欧拉路径
Solution 找一条字典序最小的欧拉路径. 用 $multiset$ 存储领接表. 欧拉路径模板传送门 Code #include<cstdio> #include<cstrin ...
- UI设计教程分享:字体变形—阴阳收缩法
阴阳师中国古代对自然规律发展变化基础因素的描述,是古代美学逻辑思维.推理分析的核心要素,也是描述万物基本要素和成因的概念之一.阴阳代表事物的对立关系,它是自然界的客观规律,是万物运动变化的本源,是人类 ...
- Date时间
var date_obj= new Date(); alert(date_obj.toLocaleString()) //2017/12/26 上午1:10:54 var date_obj2= new ...
- TP QQ 微信 微博登录
use Org\Util\QQconnect; use Org\Util\Wechatauth; use Org\Util\SaeTOAuthV2; use Org\Util\SaeTClientV2 ...
- centos6.5上配置apache + mysql + php4.4.9 + eaccelerator-0.9.5 + postgresql-8.3.13 备忘
1.apache + mysql 直接利用 yum 安装 yum -y install httpd httpd-devel mysql mysql-server httpd-manual mod_pe ...