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]表示所 ...
随机推荐
- CSS学习总结2:CSS框模型
1.CSS框模型概述 CSS框模型规定了元素框处理元素内容.内边框.边框和外边框的方式. 元素框的最内部分是实际的内容,直接包围内容的是内边距.内边距呈现了元素的背景.内边距的边缘是边框.边框以外是外 ...
- 简单使用DESeq2/EdgeR做差异分析
简单使用DESeq2/EdgeR做差异分析 Posted: 五月 07, 2017 Under: Transcriptomics By Kai no Comments DESeq2和EdgeR都 ...
- Mac下配置域名和网站测试环境
一.在 /etc/hosts 下配置相关域名 1, control+space 打开spotlight, 搜索“terminal” 2, 打开Terminal 3, 在terminal界面中输入 ...
- 第一个ASP.NET Web API (C#)程序
本文翻自http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api 绝对手工制作,如有雷同,实属巧合. 转载请注明. ...
- virtualbox 采用 NAT 还是 BRIDGE
正如标题所言,其实这两个都可以让虚拟机上网,但是还是有些差别的. 选择NAT的话, 虚拟机之间无法PING通 虚拟机可以PING通主机 主机无法PING通虚拟机 这是因为虚拟机不能在网络里拥有自己的I ...
- windows mysql 主从热备
环境说明: Master:192.168.1.200 Slave:192.168.1.210 MySQL 的 Master 配置: 配置my.ini: [mysqld] # T ...
- Ubuntu上搭建Hadoop环境(单机模式+伪分布模式) (转载)
Hadoop在处理海量数据分析方面具有独天优势.今天花了在自己的Linux上搭建了伪分布模式,期间经历很多曲折,现在将经验总结如下. 首先,了解Hadoop的三种安装模式: 1. 单机模式. 单机模式 ...
- js 正则表达式,匹配邮箱/手机号/用户名
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- mysql之多表查询和pymysql模块
一 多表查找方法 1 交叉连接:不使用任何的判断条件,生成笛卡尔积.第一个表的行数乘以第二个表的行数就等于笛卡尔积结果集的行数. mysql> select * from student,cla ...
- python 中 __name__ 的使用
1. 如果模块是被导入,__name__的值为模块名字2. 如果模块是被直接执行,__name__的值为’__main__’ Py1.py #!/usr/bin/env python def te ...