1 条题解
-
0
流淌的喷泉(Flowing Fountain) 题解
思路分析
按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。
参考代码
#include <iostream> #include <vector> #include <numeric> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; if (!(cin >> n >> q)) return 0; vector<long long> cap(n + 1), cur(n + 1, 0); for (int i = 1; i <= n; ++i) cin >> cap[i]; vector<int> nxt(n + 2, n + 1), st; for (int i = 1; i <= n; ++i) { while (!st.empty() && cap[st.back()] < cap[i]) { nxt[st.back()] = i; st.pop_back(); } st.push_back(i); } vector<int> p(n + 2); iota(p.begin(), p.end(), 0); auto find = [&](auto& self, int x) -> int { return p[x] == x ? x : p[x] = self(self, p[x]); }; for (int i = 0; i < q; ++i) { char op; cin >> op; if (op == '+') { int l; long long x; cin >> l >> x; int u = find(find, l); while (u <= n && x > 0) { long long need = cap[u] - cur[u]; if (x < need) { cur[u] += x; x = 0; } else { cur[u] = cap[u]; x -= need; p[u] = find(find, nxt[u]); u = p[u]; } } } else { int l; cin >> l; cout << cur[l] << "\n"; } } return 0; }
- 1
信息
- ID
- 8802
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者