QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#692457 | #9528. New Energy Vehicle | hkr04# | RE | 0ms | 0kb | C++14 | 2.1kb | 2024-10-31 14:32:24 | 2024-10-31 14:32:26 |
Judging History
answer
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int a[maxn], b[maxn], x[maxn], t[maxn];
int read() {
int res = 0, p = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
p = -1;
ch = getchar();
}
while (isdigit(ch))
res = res * 10 + ch - '0', ch = getchar();
return res * p;
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int T = read();
while (T--) {
int n = read(), m = read();
for (int i = 1 ;i <= n; i++)
a[i] = read(), b[i] = a[i];
vector<int> nxt(n + 1, m + 1);
vector<int> nxt_pos(m + 1);
for (int i = 1; i <= m; i++) {
x[i] = read(), t[i] = read();
}
for (int i = m; i >= 1; i--) {
nxt_pos[i] = nxt[t[i]]; // 下一个同类加油站下标
nxt[t[i]] = i; // 当前位置之后第一个加 t[i] 的加油站
}
set<pair<pair<int, int>, int>> s; // ((下一个同类加油站,类型),油量)
for (int i = 1; i <= n; i++) {
s.insert({{nxt[i], i}, a[i]});
}
ll dis = 0;
for (int i = 1; i <= m; i++) {
while (dis < x[i] && !s.empty()) {
auto p = *s.begin();
int use = min(x[i] - dis, 1ll * p.second);
dis += use;
p.second -= use;
b[p.first.second] -= use;
s.erase(s.begin());
if (p.second) { // 有剩余
s.insert(p);
}
}
if (dis < x[i]) // 无法到达
break;
auto it = s.lower_bound({{0, t[i]}, 0});
if (it != s.end() && (*it).first.second == t[i])
s.erase(it);
s.insert({{nxt_pos[i], t[i]}, a[t[i]]});
b[t[i]] = a[t[i]]; // 加满
}
for (auto p : s) {
dis += p.second;
}
cout << dis << endl;
}
return 0;
}
詳細信息
Test #1:
score: 0
Dangerous Syscalls
input:
2 3 1 3 3 3 8 1 2 2 5 2 1 2 2 1