版本比较
Day2C1
正文
增加 53 行删除 0 行
47 行未改动4849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310447 行未改动
接下来设计状态,这里为了方便,我们设 $dp_i$ 表示塔的重量恰好为 $i$ 时,塔的最大价值,于是这个问题就被转化成了 $01$ 背包问题,我们直接枚举每个物品,对于物品 $i$,它能够更新的范围一定是 $w_i$ 到 $w_i+s_i$ 即最小要放下自身,最多是自身重量加承重极限,于是有转移方程 $dp_j=\max(dp_j,dp_{j-w_i}+v_i)$,而背包的大小就是重量之和 $\sum w$。
时间复杂度 $O(nlogn+\sum s)$。
## P3422 [POI 2005] LOT-A Journey to Mars```cpp#include <bits/stdc++.h>#define int long longusing namespace std;int d[2000001], p[2000001], cnt[2000001];bool vis[2000001];signed main(){ int n; cin >> n; for (int i = 1; i <= n; i++){ cin >> p[i] >> d[i]; p[i + n] = p[i]; d[i + n] = d[i]; } for (int i = 1; i <= n * 2; i++) cnt[i] = p[i] - d[i]; for (int i = 1; i <= n * 2; i++) cnt[i] += cnt[i - 1]; deque<int> q; for (int i = n * 2; i >= 1; i--){ while (!q.empty() && q.front() > i + n - 1) q.pop_front(); while (!q.empty() && cnt[q.back()] >= cnt[i]) q.pop_back(); q.push_back(i); if (i <= n && cnt[q.front()] >= cnt[i - 1]) vis[i] = true; } d[0] = d[n]; for (int i = 1; i <= n * 2; i++) cnt[i] = p[i] - d[i - 1]; for (int i = 1; i <= n * 2; i++) cnt[i] += cnt[i - 1]; q.clear(); for (int i = 1; i <= n * 2; i++){ while (!q.empty() && q.front() < i - n) q.pop_front(); if (i > n && cnt[i] >= cnt[q.front()]) vis[i - n] = true; while (!q.empty() && cnt[q.back()] <= cnt[i]) q.pop_back(); q.push_back(i); } for (int i = 1; i <= n; i++) if (vis[i]) cout << "TAK\n"; else cout << "NIE\n"; return 0;}```