Solution
我们充分发扬人类智慧,使用特殊的状态设计,令 为选择第 头奶牛并且前 秒都有牛搞卫生的最小花费。
我们对于所有的 ,其 ,其中 代表题目中的每一头牛的花费。
我们枚举每一个 ,令 ,最后输出 。
这样快到飞起,在 的情况都只需要跑 63ms。
Code
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define dwn(i, a, b) for (int i = (a); i >= (b); i--)
#define mem memset
#define debug(x) cerr << #x << ": " << (x) << '\n'
#define HKE ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define vec vector
#define bsc basic_string
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int kMaxN = 2e5 + 1;
ll t = 1, n, S, E, f[kMaxN];
struct Cow {
ll l, r, s;
} a[kMaxN];
int main() {
HKE;
// cin >> t;
for (; t; t--) {
cin >> n >> S >> E;
rep(i, 1, n) {
cin >> a[i].l >> a[i].r >> a[i].s;
a[i].l -= S, a[i].r -= S;
a[i].l++, a[i].r++;
}
sort(a + 1, a + n + 1, [](Cow a, Cow b) {
return a.r < b.r;
});
rep(i, 1, n) {
f[i] = 1e18;
if (a[i].l == 1) {
f[i] = a[i].s;
continue;
}
dwn(j, i - 1, 0) {
if (a[j].r < a[i].l - 1) {
break;
}
f[i] = min(f[i], f[j] + a[i].s);
}
}
ll ans = 1e18;
rep(i, 1, n) {
if (a[i].r == E - S + 1) {
ans = min(ans, f[i]);
}
}
cout << (ans == 1e18 ? -1 : ans) << '\n';
}
return 0;
}