跳到正文

个人记录

P4644 题解

CuteGirlGC约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年8月10日
收录
2026年10月22日
更新
2026年11月10日
篇幅
约 3 分钟

版本

  1. v1当前

Solution

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

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

我们枚举每一个 T2,j>T1,i−1(1≤j≤)T_{2, j}>T_{1, i}-1(1\le j\le),令 fi←max⁡(fi,fj+si)f_i\gets\max(f_i,f_j+s_i),最后输出 min⁡T2,i=Efi\min\limits_{T_{2, i}=E} f_i。

这样快到飞起,在 n≤104n \le 10^4 的情况都只需要跑 63ms。

Code

C++
#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;
}
原文