CF822C Hacker, pack your bags!

思路:

对于一个区间[l, r],只需枚举所有满足r' < l并且二者duration之和为x的区间[l', r'],寻找其中二者cost之和最小的即可。于是可以开一个数组a[],a[i]表示所有能与i配对的区间(duration为x - i)的最小花费。计算的时候根据区间左端点由小到大依次更新就可以满足区间不重叠的限制,具体参见代码。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 using namespace std;
 5 
 6 typedef long long ll;
 7 const ll MAXN = 200005, INF = 0x3f3f3f3f3f3f3f3f;
 8 typedef pair<ll, ll> P;
 9 vector<P> L[MAXN], R[MAXN];
10 ll a[MAXN];
11 
12 int main()
13 {
14     ll n, x, l, r, c;
15     scanf("%I64d %I64d", &n, &x);
16     fill(a, a + MAXN, INF);
17     for (int i = 0; i < n; i++)
18     {
19         scanf("%I64d %I64d %I64d", &l, &r, &c);
20         L[l].push_back({r - l + 1, c});
21         R[r].push_back({r - l + 1, c});
22     }
23     ll ans = INF;
24     for (int i = 1; i < MAXN; i++)
25     {
26         for (auto it : L[i])
27         {
28             if (it.first >= x) continue;
29             ans = min(ans, a[x - it.first] + it.second);
30         }
31         for (auto it : R[i])
32             a[it.first] = min(a[it.first], it.second);
33     }
34     cout << (ans == INF ? -1 : ans) << endl;
35     return 0;
36 }

 

转载于:https://www.cnblogs.com/wangyiming/p/7125988.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值