codeforces 822 C. Hacker, pack your bags!(思维+dp)
来源:互联网 发布:linux ioremap 编辑:程序博客网 时间:2024/06/03 13:53
题目链接:http://codeforces.com/contest/822/submission/28248100
题目大意:给你n个旅券,上面有开始时间l,结束时间r,和花费cost,要求选择两张时间不相交的旅券时间长度相加为x,且要求花费最少。
题解:可以把每个点拆成两个点,这样在遍历的时候就可以知道这个是起点还是终点,如果是终点就可以更新dp了,如果是起点就更新答案。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define inf 1000000000000
using
namespace
std;
const
int
M = 2e5 + 10;
typedef
long
long
ll;
struct
TnT {
int
sta , cau , flag;
ll val;
}a[M << 1];
bool
cmp(TnT x , TnT y) {
if
(x.sta == y.sta)
return
x.flag > y.flag;
return
x.sta < y.sta;
}
ll dp[M];
//dp[i]存储之前状态区间长度为i的最小花费
int
main() {
int
n;
ll x;
scanf
(
"%d%lld"
, &n , &x);
int
cnt = 0;
for
(
int
i = 0 ; i < n ; i++) {
int
l , r;
ll c;
scanf
(
"%d%d%lld"
, &l , &r , &c);
a[cnt].sta = l , a[cnt].flag = 1 , a[cnt].cau = r - l + 1 , a[cnt].val = c;
a[++cnt].sta = r , a[cnt].flag = -1 , a[cnt].cau = r - l + 1 , a[cnt].val = c;
cnt++;
}
sort(a , a + cnt , cmp);
for
(
int
i = 0 ; i < M ; i++) dp[i] = inf;
ll ans = inf;
for
(
int
i = 0 ; i < cnt ; i++) {
if
(a[i].flag == 1) {
ll sum = x - a[i].cau;
if
(sum >= 0) {
if
(dp[sum] < inf) ans = min(ans , a[i].val + dp[sum]);
}
}
else
{
dp[a[i].cau] = min(dp[a[i].cau] , a[i].val);
}
//这里的标记决定了dp是否要更新,显然在区间结束时就可以更新dp了
}
if
(ans < inf)
printf
(
"%lld\n"
, ans);
else
printf
(
"-1\n"
);
return
0;
}
阅读全文
0 0
- codeforces 822 C. Hacker, pack your bags!(思维+dp)
- Codeforces 822C Hacker, pack your bags!(思维+技巧)
- Codeforces 822 C. Hacker, pack your bags! 思维
- codeforces #422 C. Hacker, pack your bags!(贪心dp)
- codeforces 822 C Hacker, pack your bags!
- CodeForces 822C Hacker, pack your bags!
- codeforces 882C Hacker, pack your bags!
- CF822C:Hacker, pack your bags!(思维)
- Codeforces 822C Hacker, pack your bags!【排序+二分】
- 【Codeforces 822 C. Hacker, pack your bags!】+ pair
- Codeforces 822C Hacker pack your bags! 区间
- Codeforces Round #422 C. Hacker, pack your bags! (二分)
- Hacker, pack your bags!(线段+dp)
- codeforces Hacker, pack your bags!
- Hacker, pack your bags! CodeForces
- 822C Hacker, pack your bags!
- 822C Hacker, pack your bags!
- Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 思维排序或二分
- COJ 2047 白旭东の几何之路
- 175. Combine Two Tables--left join
- (二)设置PDF纸张并在指定位置添加文字
- mysql 创建数据库字段类型
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 2)
- codeforces 822 C. Hacker, pack your bags!(思维+dp)
- 背包问题-LintCode
- Android butterknife view注入框架
- 正则表达式"\\s+"
- 24天学会设计模式------责任链模式
- BaseModel
- JavaScript事件委托的技术原理
- Scala JDBC 查询和更新MySQL
- Spring容器ApplicationContext实现和配置WebApplicationContext