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 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 or rj < 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 to 2.
题目大意 给定n个区间,每个区间有个费用,选择两个不相交(端点也不能重叠)的区间使得它们的长度总和恰好为x,并且使得它们的费用和最小。如果无解输出-1。
根据常用套路,按照区间的长度进行分组。然后按照端点的大小(反正按左端点还是右端点都是一样的)进行排序。再拿两个数组跑一道前缀最小值和后缀最小值。
现在枚举每一条旅行方案,在和它的长度之和恰好为x的另一个数组中,可以用lower_bound和upper_bound快速找出两侧合法的的最后一个和第一个,然后更新一下就好了(详细可以看代码)。

Code
/**
* Codeforces
* Problem#822C
* Accepted
* Time:124ms
* Memory:16416k
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
const double eps = 1e-;
const int binary_limit = ;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} typedef class Trip {
public:
int l;
int r;
int fee; Trip(int l = , int r = , int fee = ):l(l), r(r), fee(fee) { } boolean operator < (Trip b) const {
return l < b.l;
}
}Trip; boolean operator < (const int& b, const Trip& a) {
return b < a.l;
} int n, x;
vector<Trip>* a;
vector<int>* minu; // in order
vector<int>* mind; // in opposite order inline void init() {
readInteger(n);
readInteger(x);
a = new vector<Trip>[x];
minu = new vector<int>[x];
mind = new vector<int>[x];
for(int i = , l, r, c; i <= n; i++) {
readInteger(l);
readInteger(r);
readInteger(c);
int len = r - l + ;
if(len >= x) continue;
a[len].push_back(Trip(l, r, c));
}
} int res = inf;
inline void solve() {
for(int i = ; i < x; i++)
if(!a[i].empty()) {
sort(a[i].begin(), a[i].end());
minu[i].resize(a[i].size());
mind[i].resize(a[i].size());
for(int j = ; j < (signed)a[i].size(); j++) {
if(j == ) minu[i][j] = a[i][j].fee;
else minu[i][j] = min(a[i][j].fee, minu[i][j - ]);
}
for(int j = (signed)a[i].size() - ; j >= ; j--) {
if(j == a[i].size() - ) mind[i][j] = a[i][j].fee;
else mind[i][j] = min(a[i][j].fee, mind[i][j + ]);
}
} for(int i = ; i < x; i++) {
// cout << i << ":" << endl;
if(!a[i].empty()) {
for(int j = ; j < a[i].size(); j++) {
// cout << a[i][j].l << " " << a[i][j].r << endl;
Trip &t = a[i][j];
int len = t.r - t.l + ;
int ulen = x - len;
int pos = upper_bound(a[ulen].begin(), a[ulen].end(), t.r) - a[ulen].begin();
if(pos != a[ulen].size()) smin(res, t.fee + mind[ulen][pos]);
pos = lower_bound(a[ulen].begin(), a[ulen].end(), t.l - ulen + ) - a[ulen].begin() - ;
if(pos != -) smin(res, t.fee + minu[ulen][pos]);
}
}
} if(res == inf)
puts("-1");
else
printf("%d\n", res);
} int main() {
init();
solve();
return ;
}
Codeforces 822C Hacker, pack your bags! - 贪心的更多相关文章
- 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 ...
- Codeforces 822C Hacker, pack your bags!(思维)
题目大意:给你n个旅券,上面有开始时间l,结束时间r,和花费cost,要求选择两张时间不相交的旅券时间长度相加为x,且要求花费最少. 解题思路:看了大佬的才会写!其实和之前Codeforces 776 ...
- CodeForces 822C Hacker, pack your bags!
题意 给出一些闭区间(始末+代价),选取两段不重合区间使长度之和恰为x且代价最低 思路 相同持续时间的放在一个vector中,内部再对起始时间排序,从后向前扫获取对应起始时间的最优代价,存在minn中 ...
- 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 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 ...
- CF822C Hacker, pack your bags!(思维)
Hacker, pack your bags [题目链接]Hacker, pack your bags &题意: 有n条线段(n<=2e5) 每条线段有左端点li,右端点ri,价值cos ...
- Codeforces822 C. Hacker, pack your bags!
C. Hacker, pack your bags! time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(二分写法)
[题目链接]:http://codeforces.com/contest/822/problem/C [题意] 有n个旅行计划, 每个旅行计划以开始日期li,结束日期ri,以及花费金钱costi描述; ...
- codeforces 822 C. Hacker, pack your bags!(思维+dp)
题目链接:http://codeforces.com/contest/822/submission/28248100 题解:多维的可以先降一下维度sort一下可以而且这种区间类型的可以拆一下区间只要加 ...
随机推荐
- Selenium基础知识(一)环境与搜索
所需环境: 1.python2.7 + pycharm 2.Selenium 3.浏览器驱动(IEDriverServer.exe) 这里使用的是ie浏览器 将驱动放到一个环境变量路径,这里直接放在 ...
- 答案在哪里?action config/Interceptor/class/servlet
项目已提测,这两天我们都集中精力梳理外包团队给我司研发的这个三方支付系统的代码逻辑.今天下午爱琴海会议室,开发组里一同学分享他对支付结果回调的梳理成果. 支付结果回调的整体时序是:支付渠道方处理完用户 ...
- 42.PHP--电商网站的询价插件
windows下使用php自带的mail函数实现简单的邮件发送实例(QQ亲试成功) 功能实现:通过中间邮箱把客户的需求和产品信息全部发送到公司的业务员邮箱 环境及工具: win7_64bit phps ...
- [提权] 脏牛漏洞 Dirty COW CVE-2016-5195 2.6.22 < 3.9 (x86/x64)
/* * (un)comment correct payload first (x86 or x64)! * * $ gcc cowroot.c -o cowroot -pthread * $ ./c ...
- Boot-col-sm布局
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- redis的key越来越多,对速度是否有影响
---恢复内容开始--- redis存储key是用字典对象的,查询性能几乎和数量级无关,只要保证内存够用就可以了,如果内存不够,会把内存和swap空间交换,这种情况下就会很影响性能,会读取出现读取磁盘 ...
- Service Fabric本地开发部署修改数据目录
以修改5节点非安全模式为例: 在C:\Program Files\Microsoft SDKs\Service Fabric\ClusterSetup\NonSecure\FiveNode目录下,修改 ...
- SpringBoot返回json格式到浏览器上,出现乱码问题
在对应的Controller上的requestMapping中添加: @RestController@EnableAutoConfiguration@RequestMapping(value = &q ...
- Core Java Fundation
http://www.cnblogs.com/cmfwm/p/7671188.html http://blog.csdn.net/fuckluy/article/details/50614983 ht ...
- SQLAllocHandle
函数定义: 顾名思义,该函数就是用来分配句柄的,句柄类型参考参数详解. SQLRETURN SQLAllocHandle( SQLSMALLINT HandleType, SQLHANDLE ...