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]表示所 ...
随机推荐
- poj 2528(线段树+离散化) 市长的海报
http://poj.org/problem?id=2528 题目大意是市长竞选要贴海报,给出墙的长度和依次张贴的海报的长度区间(参考题目给的图),问最后你能看见的海报有几张 就是有的先贴的海报可能会 ...
- OSX.PackageManager-Homebrew
How to install pip on mac? http://stackoverflow.com/questions/12092306/how-to-install-scipy-with-pip ...
- 虚拟机安装centos7, 再安装gitlab 简单步骤
先安装Linux centos7(朋友贡献的. Linux官网有下) 我自己用vm安装的. 未出现特殊状况 gitlab的搭建 安装基础包 yum -y install curl policycore ...
- Luogu 1415-拆分数列-动态规划
Solution 首先要找到使得最后一个数最小, 只需定义一个数组$pre[i]$ 从区间$[pre[i], i]$表示的数, 是最小的能使前面的数递增的方案. $[ pre[n], n]$即为最小的 ...
- 微信小程序播放视频发送弹幕效果
首先.先来看一下效果图 然后.再看一下官方文档API对video的说明 相关属性解析: danmu-list:弹幕列表 enable-danmu:是否显示弹幕 danmu-btn:弹幕按钮 contr ...
- centos php 运行环境搭建
一.安装apache httpd rpm -qa|grep httpd //检查是否安装apache rpm -e 包名 --nodeps //若有则删除 PS:我没有删除,直接用的服务器原来的. y ...
- gsl库安装
下载ftp://ftp.gnu.org/gnu/gsl/ 下载后解压,可以按照文件夹中INSTALL文件的指导,进入解压文件夹"gsl-2.4"执行以下5步: ./configur ...
- jvm层面锁优化+一般锁的优化策略
偏向锁: 首先了解对象头MARK指针(对象头标记,32位): 存储GC标记,对象年龄,对象Hash,锁信息(锁记录的指针,偏向锁线程的ID) 大部分情况是没有竞争的,所以可以通过偏向来提高性能 所谓的 ...
- 9月list
开学了,我已经是大三的老学姐了,难受! 哇,时间过得好快啊,感觉自己快毕业了,肿么办!!! 9月了,快一年了,其实很多东西都变了,比如你. 9月4日的list:
- 69.查看APP沙盒缓存的内容文件
第一步:链接真机设备,点击Xcode ,按command+shift+2 弹出电脑所运行的APP列表 第二步:选中你需要查看的APP,点击最下面! 类似于设置图标的按钮! 点击第二个Download ...