P4644 题解

· · 个人记录

Solution

我们充分发扬人类智慧,使用特殊的状态设计,令 f_i 为选择第 i 头奶牛并且前 T_{2, i} 秒都有牛搞卫生的最小花费。

我们对于所有的 T_{1, i}=S,其 f_{i}=s_i,其中 s_i 代表题目中的每一头牛的花费。

我们枚举每一个 T_{2, j}>T_{1, i}-1(1\le j\le),令 f_i\gets\max(f_i,f_j+s_i),最后输出 \min\limits_{T_{2, i}=E} f_i。

这样快到飞起,在 n \le 10^4 的情况都只需要跑 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;
}