QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#697041 | #9528. New Energy Vehicle | ranxi | RE | 0ms | 0kb | C++23 | 2.4kb | 2024-11-01 09:58:02 | 2024-11-01 09:58:03 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
//#define int ll
const int N = 2e5 + 5;
struct node{
int x;
int id;
};
void solve() {
int n, m;
cin >> n >> m;
vector<int>a(n+1),b(n+1);
vector<int>vc[n+1];
vector<node>info(m + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = a[i];
}
for (int i = 1; i <= m; i++) {
cin >> info[i].x >> info[i].id;
vc[info[i].x].push_back(info[i].id);
}
//vc 电池编号:在哪充电
//q里面放最近的充电站,给谁充电
//q小根堆,从最近的充电站的电池开始用
int ans = 0;
priority_queue<PII, vector<PII>, greater<PII>> q;
for (int i = 1; i <= n; i++) {
if (vc[i].size()) {
q.push({vc[i][0], i});
vc[i].erase(vc[i].begin());
} else
q.push({2e9, i});
}
int flag = 0;
for (int i = 1; i <= m; i++) {
int dis = info[i].x - ans;//到充电站的成本
int t = 0;
while (!q.empty()) {
if (t + b[q.top().second] < dis) {
t += b[q.top().second];
b[q.top().second] = 0;
q.pop();
} else {
b[q.top().second] -= (dis - t);
if (b[q.top().second] == 0)
q.pop();
t = dis;
break;
}
}
// 没走到加油站
if (t < dis) {
flag = 1;
ans += t;
break;
}
// 走到加油站之后
if (!q.empty() && q.top().second == info[i].id) q.pop();//要给你充电
if (vc[info[i].id].size()) {
q.push({vc[info[i].id][0], info[i].id});
vc[info[i].id].erase(vc[info[i].id].begin());
} else
q.push({2e9, info[i].id});//永远充不了电了
b[info[i].id] = a[info[i].id];
ans = info[i].x;
}
if (!flag) {
while (!q.empty()) {
ans += b[q.top().second];
q.pop();
}
}
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
}
详细
Test #1:
score: 0
Runtime Error
input:
2 3 1 3 3 3 8 1 2 2 5 2 1 2 2 1