A - Hacker, pack your bags! CodeForces - 822C(思路,排序)
2018-03-05 16:09
417 查看
C. Hacker, pack your bags!time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIt'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!InputThe 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.OutputPrint 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.Examplesinput
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 2e9+10
typedef long long ll;
using namespace std;
int n, x;
ll a[200010];//存储长度为i的行程里cost最低的值
typedef struct{
int pos;
ll cost;
int type;
int len;
}Node;
Node node[400010];//注意这里存了2*2*10^5bool cmp(const Node a, const Node b)
{
if(a.pos == b.pos)
return a.type<b.type;
else return a.pos<b.pos;
}
int main()
{
cin >> n >> x;
for(int i = 0; i <= x; i++){
a[i] = INF;
}
int t=0, l, r, cost;
for(int i = 0; i < n; i++){
cin >> l >> r >> cost;
node[t].len=r-l+1;node[t].pos=l;node[t].cost=cost;node[t].type=-1;
t++;
node[t].len=r-l+1;node[t].pos=r;node[t].cost=cost;node[t].type=1;
t++;
}
sort(node, node+t, cmp);
ll dist, mini, ans=INF;
for(int i = 0; i < t; i++){
if(node[i].type == -1){//左边界
if(x > node[i].len){
ans = min(ans, a[x-node[i].len]+node[i].cost);
}
}
else{//右边界
a[node[i].len] = min(a[node[i].len], node[i].cost);
}
}
if(ans == INF){
cout << "-1" << endl;
}
else
cout << ans << endl;
return 0;
}
4 5 1 3 4 1 2 5 5 6 1 1 2 4output
5input
3 2 4 6 3 2 4 1 3 5 4output
-1NoteIn 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.题意:给出若干区间l,r,和相应的权cost,求两个不相交的区间且权值和最大。思路:太暴力会超时。这里将所有的边界点升序排序,并且保证值相同时左边界总在右边界前面。然后O(n)遍历这些点,遇到左边界的时候更新a[](记录len为i时的最少花费),遇到右边界的时候更新ans(判断如果加入当前的区间,权值和会不会更小)。注意node[]数组要开两倍的两倍,ans值超过int最大范围代码:#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 2e9+10
typedef long long ll;
using namespace std;
int n, x;
ll a[200010];//存储长度为i的行程里cost最低的值
typedef struct{
int pos;
ll cost;
int type;
int len;
}Node;
Node node[400010];//注意这里存了2*2*10^5bool cmp(const Node a, const Node b)
{
if(a.pos == b.pos)
return a.type<b.type;
else return a.pos<b.pos;
}
int main()
{
cin >> n >> x;
for(int i = 0; i <= x; i++){
a[i] = INF;
}
int t=0, l, r, cost;
for(int i = 0; i < n; i++){
cin >> l >> r >> cost;
node[t].len=r-l+1;node[t].pos=l;node[t].cost=cost;node[t].type=-1;
t++;
node[t].len=r-l+1;node[t].pos=r;node[t].cost=cost;node[t].type=1;
t++;
}
sort(node, node+t, cmp);
ll dist, mini, ans=INF;
for(int i = 0; i < t; i++){
if(node[i].type == -1){//左边界
if(x > node[i].len){
ans = min(ans, a[x-node[i].len]+node[i].cost);
}
}
else{//右边界
a[node[i].len] = min(a[node[i].len], node[i].cost);
}
}
if(ans == INF){
cout << "-1" << endl;
}
else
cout << ans << endl;
return 0;
}
相关文章推荐
- Codeforces 822C Hacker, pack your bags!【排序+二分】
- 【线段问题创新思路】Hacker, pack your bags! CodeForces - 822C
- CodeForces - 822C Hacker, pack your bags!(区间端点处理技巧+思维)
- CodeForces 822C Hacker, pack your bags!
- Codeforces 822C - Hacker, pack your bags!(二分)
- Codeforces 822C Hacker, pack your bags!(思维+技巧)
- Hacker, pack your bags! CodeForces - 822C
- Codeforces 822C Hacker, pack your bags! - 贪心
- Codeforces 822C: Hacker, pack your bags!【二分查找】【后缀最小值】
- Codeforces 822 C. Hacker, pack your bags! 思维
- C - Hacker, pack your bags! Codeforces(贪心)
- 【Codeforces 822 C. Hacker, pack your bags!】+ pair
- 822C Hacker, pack your bags!
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序,贪心
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序, 贪心, 暴力(居然可以n方暴力)
- codeforces 822 C. Hacker, pack your bags!(思维+dp)
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 思维排序或二分
- codeforces 822 C Hacker, pack your bags!
- Codefroces 822C Hacker, pack your bags!
- 【L,R的特点】codeforce 822C Hacker, pack your bags!