CF422 div2 C. Hacker, pack your bags!
来源:互联网 发布:梁朝伟愿你知我心 编辑:程序博客网 时间:2024/06/06 10:00
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 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 51 3 41 2 55 6 11 2 4
5
3 24 6 32 4 13 5 4
-1
一个数组记录当前存在的天数为v的一个最小花费,初始为无穷大。
对于当前点,先看是左端点的,如果存在x-v的一个最小花费区间,那么更新答案。
然后看右端点,直接更新天数为v的一个最小花费。
这样就能够避免一后效性和重复了。
#include<bits/stdc++.h>using namespace std;const int MAXN=2e5+7;const int inf = 2e9+1;int n,m;struct node{ int l,r; int cost; int v; int flag; bool operator < (const node &a)const { return flag < a.flag; }} p[MAXN];vector<node>ha[MAXN];int vis[MAXN];int main(){ scanf("%d%d",&n,&m); for(int i = 0 ; i < n ; ++i) { scanf("%d%d%d",&p[i].l,&p[i].r,&p[i].cost); p[i].v = p[i].r - p[i].l + 1; p[i].flag = 1; ha[p[i].l].push_back(p[i]); p[i].flag = 2; ha[p[i].r].push_back(p[i]); } for(int i = 1 ; i <= 200000; ++i) { //sort(ha[i].begin(),ha[i].end()); vis[i] = inf; } int MIN = inf; for(int i = 1 ; i <= 200000 ; ++i) { for(int j = 0 , l = ha[i].size() ; j < l ;++j) { int v = ha[i][j].v; int cost = ha[i][j].cost; int flag = ha[i][j].flag; if(flag == 1) { if(v < m && vis[m-v] != inf) { MIN = min(MIN,cost + vis[m-v]); } } else vis[v] = min(vis[v],cost); } } if(MIN == inf)puts("-1"); else printf("%d\n",MIN);}
- CF422 div2 C. Hacker, pack your bags!
- Hacker, pack your bags!
- cf C. Hacker, pack your bags!
- codeforces 822 C Hacker, pack your bags!
- Codeforces822 C. Hacker, pack your bags!
- 822C Hacker, pack your bags!
- Round 422 C. Hacker, pack your bags!
- codeforces 882C Hacker, pack your bags!
- CodeForces 822C Hacker, pack your bags!
- 822C Hacker, pack your bags!
- codeforces Hacker, pack your bags!
- cf822c Hacker,Pack your bags
- Hacker, pack your bags! CodeForces
- `Hacker, pack your bags!
- Codeforces Round #422 C. Hacker, pack your bags! (二分)
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags!
- CodeFroces 822C.Hacker, pack your bags!(sorting+线性枚举)
- Codeforces 822C Hacker, pack your bags!(思维+技巧)
- 文件格式之gif
- [BZOJ]2154: Crash的数字表格&&2693: jzptab
- 自动化脚本编写实例-python
- jquery detach()和remove()的区别
- tomcat工作原理浅析
- CF422 div2 C. Hacker, pack your bags!
- android开发环境配置
- 服务器通过mavn部署项目至tomcat脚本
- What is Action in C#?
- (Java)使用Java发送带参数的http(GET)请求,获取json数据
- 简单的方法判断是不是数字
- linux调试----addr2line将(转换为地址及所在的文件及行数)
- java中的集合
- idea多模块项目发布,公共模块的代码没有改变