版本比较
Shopping Plans Trick
正文
增加 13 行删除 13 行
55 行未改动5657585960606162636433 行未改动9899100101102102103104105106211 行未改动全部展开31831932032132232232332432532632632732832933028 行未改动35936036136236336336436536636736836836937037137137237337437544 行未改动42042142242342442442542642742842943043143243343243343443543643741 行未改动47948048148248348348448548648734 行未改动522523524525526526527528529530531532532533534535536144 行未改动全部展开55 行未改动
### 2.1.1 Shopping Plans on Multiset 1
> 给你一个大小为 $n$ 的可重集 $S$,求大小为 $p$ 的前 $k$ 小子集和。> >> $1 \le n,k,p \le 3 \times 10^5$。
:::::success[题解]先对 $S$ 排序,则 $S_0$ 为选择前 $p$ 项。33 行未改动void _main() { cin >> n >> k >> p; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1);
long long sum = 0; for (int i = 1; i <= p; i++) sum += a[i]; q.emplace(p, n + 1, n + 1, sum); while (k--) {211 行未改动全部展开 });#define A(i, j) (a[i].h[j]) q.emplace(1, 2, res - A(1, 1) + A(1, 2)); // 对(1, 1, res)做S1 while (--k) { int x = q.top().x, y = q.top().y; long long v = q.top().val; int x = q.top().x, y = q.top().y; long long v = q.top().val; q.pop(), res += v; if (y < a[x].len) q.emplace(x, y + 1, v - A(x, y) + A(x, y + 1)); // S1 if (x < n) q.emplace(x + 1, 2, v - A(x + 1, 1) + A(x + 1, 2)); // S2 if (x < n && y == 2) q.emplace(x + 1, 2, v - A(x + 1, 1) - A(x, 2) + A(x + 1, 2) + A(x, 1)); // S3 if (x < n && y == 2) q.emplace(x + 1, 2, v - A(x + 1, 1) - A(x, 2) + A(x + 1, 2) + A(x, 1)); // S3 } cout << res;}```::::28 行未改动 node(int a = 0, int b = 0, int c = 0, long long d = 0) : i(a), j(b), k(c), w(d) {} bool operator< (const node& a) const {return w > a.w;} }; priority_queue<node> q;
vector<int> a; vector<long long> ans; int l, r; void add(int x) {a.emplace_back(x);}
void init() { int n = a.size(); if (l > r || l > n) return; // 注意特判 if (l > r || l > n) return; // 注意特判 sort(a.begin(), a.end()); long long sum = 0; for (int i = 1; i < l; i++) sum += a[i - 1]; if (l == 0) q.emplace(0, n + 1, n + 1, 0), l++; // 特判l=044 行未改动 } res += A(i, 1); } cout << res << '\n'; if (A(1, 2) != null) q.emplace(1, 2, res - A(1, 1) + A(1, 2)); if (A(1, 2) != null) q.emplace(1, 2, res - A(1, 1) + A(1, 2)); int cnt = 1; while (!q.empty()) { int x = q.top().x, y = q.top().y; long long v = q.top().val; q.pop(); cout << v << '\n'; if (++cnt >= k) break; if (A(x, y + 1) != null) q.emplace(x, y + 1, v - A(x, y) + A(x, y + 1)); if (A(x + 1, 2) != null && x < n) q.emplace(x + 1, 2, v - A(x + 1, 1) + A(x + 1, 2)); if (A(x, 2) != null && A(x + 1, 2) != null && x < n && y == 2) q.emplace(x + 1, 2, v - A(x + 1, 1) - A(x, 2) + A(x + 1, 2) + A(x, 1)); } if (A(x, 2) != null && A(x + 1, 2) != null && x < n && y == 2) q.emplace(x + 1, 2, v - A(x + 1, 1) - A(x, 2) + A(x + 1, 2) + A(x, 1)); } while (cnt++ < k) cout << -1 << '\n';}```::::41 行未改动
::::info[代码]```cppconst int N = 3e5 + 5;int n, m, k, u, v, s, t, p[N]; int n, m, k, u, v, s, t, p[N];long long w;
int tot = 0, head[N];struct Edge {34 行未改动 } edge[N << 1]; inline void add_edge(int u, int v, long long w) { edge[++tot].next = head[u], edge[tot].to = v, edge[tot].dis = w, head[u] = tot; }
struct node { int to; long long dis; node(int x = 0, long long y = 0.0) : to(x), dis(y) {} bool operator< (const node& other) const {return dis > other.dis;} };
int fa[N]; long long dis[N]; priority_queue<node> q; void dijkstra(int s) {144 行未改动全部展开