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