我是小学生 • 1天前
using namespace std;
int main() {
int n, sum = 0, way, time, price;
queue< pair<int, int> > q;
cin >> n;
while (n--) {
cin >> way >> price >> time;
if (way == 0) {
// 乘坐地铁,添加优惠票到队列,累加花费
q.push({price, time});
sum += price;
} else {
// 先移除所有过期的优惠票(超过45分钟)
while (!q.empty() && time - q.front().second > 45) {
q.pop();
}
// 检查所有有效优惠票,寻找第一个符合条件的
bool used = 0;
queue< pair<int, int> > temp; // 临时存储未使用的票
// 遍历所有有效票,确保所有票都被处理
while (!q.empty()) {
auto cur = q.front();
q.pop();
if (!used && price <= cur.first) {
// 找到第一个符合条件的票,标记使用
used = 1;
} else {
// 不符合条件的票,或已找到可用票后剩余的票,放入临时队列
temp.push(cur);
}
}
// 将临时队列中未使用的票放回原队列
while (!temp.empty()) {
q.push(temp.front());
temp.pop();
}
// 若未使用优惠票,则支付公交车费
if (!used) {
sum += price;
}
}
}
cout << sum;
return 0;
}
评论:
请先登录,才能进行评论