洛谷
有 \(n\) 个商品,每一样商品都有收益 \(P_i\) 和过期时间 \(D_i\),每天只卖一个商品,一旦超过了过期时间,商品就不能再卖。
收益由大到小枚举,每个商品卖的时间尽量设置在后面,用并查集维护当前商品的日期最晚还能设置在哪里。
inline ll READ() { ll x = 0, f = 1; char c = getchar(); while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') f = -f, c = getchar(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar(); return x * f; } int n; struct node { int v, i; bool operator < (node &a) const { return v > a.v; } }a[N]; int fa[N]; int ans; int Find(int k) {return k == fa[k]? k: fa[k] = Find(fa[k]);} int main() { for (; scanf ("%d", &n) != EOF; ) { memset (a, 0, sizeof a); int mx; for (int i = 1; i <= n; i++) mx = max((a[i] = (node){READ(), READ()}).i, mx); for (int i = 1; i <= mx; i++) fa[i] = i; sort (a + 1, a + 1 + n); ans = 0; for (int i = 1, tmp; i <= n; i++) if ((tmp = Find(a[i].i)) > 0) { fa[tmp] = tmp - 1; ans += a[i].v; } printf ("%d\n", ans); } return 0; }