马上CSP2024了,做题ing... (
题目描述
思路
1.用结构体双端队列存票,用双端队列的原因是后面要遍历
2.结构体元素:price+time+used
3.过期的票要及时pop
4.不要一边遍历一边pop,用used标记
代码
#include <bits/stdc++.h>
using namespace std;
struct Ticket {
int price, time;
bool used;
};
deque<Ticket> q;
int n, cost;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
int op, price, time;
cin >> op >> price >> time;
if (op == 0) {
cost += price;
q.push_back({price, time + 45, false});
} else {
while (!q.empty() && q.front().time < time) q.pop_front();
bool f = false;
for (int j = 0; j < (int)q.size(); ++j) {
if (q[j].price >= price && q[j].used == 0) {
f = true;
q[j].used = 1;
break;
}
}
if (!f) cost += price;
}
}
cout << cost;
return 0;
}