#include<bits/stdc++.h> usingnamespace std; using ll = longlong;
voideachT() { int n, m; cin >> n >> m; vector<ll> a(n + 1); vector<ll> X(m + 2), T(m + 3); for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= m; i++) { cin >> X[i] >> T[i]; } X[m + 1] = 2e18; vector<vector<int>> adj(n + 1); for (int i = 1; i <= n; i++) { adj[i].push_back(m + 1); } for (int i = m; i >= 1; i--) { adj[T[i]].push_back(i); }
vector<ll> b = a; set<array<int, 2>> alive; auto upd = [&](int i) { if (b[i] > 0) { alive.insert({adj[i].back(), i}); } };
for (int i = 1; i <= n; i++) { upd(i); } int x = 0; int tx = 1; while (!alive.empty()) { auto [pos, i] = *alive.begin(); ll now = min<ll>(b[i], X[tx] - x); if (b[i] < X[tx] - x) { alive.erase({pos, i}); b[i] = 0; } else { b[i] -= now; int j = T[tx]; alive.erase({adj[j].back(), j}); adj[j].pop_back(); alive.insert({adj[j].back(), j}); b[j] = a[j]; tx++; } x += now; } cout << x << '\n'; }
intmain() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int T = 1; cin >> T; while (T--) { eachT(); } }
以上为野羊题解。。
一个电瓶没电后所有和这个电瓶相关的信息 都同时消失,充电后 都同时出现,说明每个电瓶的信息具有 强相关性,因此将其存为 链表,再用 set 存储每个链表的头。
vector<int> a(n + 1); for (int i = 0; i < n; i++) { cin >> a[i]; }
vector<vector<int>> vec(n + 1); // 每个电瓶有哪些充电桩 vector<array<int, 2>> b(m + 1); for (int i = 0; i < m; i++) { int x, t; cin >> x >> t; t--; vec[t].push_back(i); b[i] = { x, t }; } b[m] = { inf, n };
set<array<int, 2>> Q; vector<vector<int>::iterator> head(n + 1); for (int i = 0; i <= n; i++) { vec[i].push_back(m); // 所有的电瓶最后都要连到一个无穷远的充电桩 head[i] = vec[i].begin(); Q.insert({ *head[i], i }); }
auto now = a; int res = 0; for (auto [x, t] : b) { // 枚举每个充电桩 int i; // 当前正在消耗的电瓶编号 while (!Q.empty() && res + now[i = (*Q.begin())[1]] < x) { // 跑不到充电桩 res += now[i]; now[i] = 0; // 把所有电用完 Q.erase(Q.begin()); } if (Q.empty()) break;
ll dis = x - res; now[i] -= dis; res += dis;
Q.erase({ *head[t], t }); head[t]++; Q.insert({ *head[t], t });
#include<bits/stdc++.h> usingnamespace std; using ll = longlong; const ll N = 2e5 + 10; const ll inf = 1e18;
voideachT() { int n, m, k, w; cin >> n >> m >> k >> w; vector<int> a(n), b(m + 2); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); for (int i = 1; i <= m; i++) { cin >> b[i]; } b[0] = 0; b[m + 1] = w + 1; sort(b.begin(), b.end());
bool ok = 1; vector<int> res;
auto solve = [&](vector<ll>& a, int& l, int& r) -> void { vector<int> pos; int len = a.size(); for (int i = 0; i < len; ) { pos.push_back(a[i] + k - 1); int p = upper_bound(a.begin(), a.end(), a[i] + k - 1) - a.begin(); if (a[p] == r) { for (auto x : pos) { res.push_back(x - k + 1); } return; } elseif (a[p] == inf) { pos[pos.size() - 1] = r - 1; for (int j = pos.size() - 1; j >= 0; j--) { if (pos[j] - k + 1 <= l) { ok = 0; return; } if (j >= 1 && pos[j] - k + 1 <= pos[j - 1]) { pos[j - 1] = pos[j] - k; } else { for (auto x : pos) { res.push_back(x - k + 1); } return; } } } else { i = p; } } };
int now = 1; vector<ll> aa; for (int i = 0; i < n;) { if (!ok) { break; } while (a[i] > b[now]) { now++; } while (a[i] <= b[now]) { aa.push_back(a[i]); i++; if (i >= n) { break; } } aa.push_back(b[now]); aa.push_back(inf); solve(aa, b[now - 1], b[now]); aa.clear(); now++; } if (!ok) { cout << -1 << endl; } else { cout << res.size() << endl; for (auto x : res) { cout << x << " "; } cout << endl; }