A50005 人工 + AI 题解
2025-08-14 11:14:52
发布于:广东
19阅读
0回复
0点赞
注: 主体框架为手打
#include <vector>
#include <queue>
using namespace std;
// 定义优惠票结构体,包含票价和乘车时间
struct Ticket {
int price; // 票价
int time; // 乘车时间(分钟数)
};
int main() {
int n; // 乘车记录数量
cin >> n;
// 使用队列存储优惠票,按照先进先出原则处理
queue<Ticket> tickets;
int total_cost = 0; // 总花费
for (int i = 0; i < n; ++i) {
int type, price, time;
cin >> type >> price >> time; // 读取每条记录
if (type == 0) { // 地铁记录
// 将地铁票加入优惠票队列
tickets.push({price, time});
// 地铁票需要付费,累加到总花费
total_cost += price;
} else { // 公交车记录
bool used = false; // 标记是否使用了优惠票
queue<Ticket> temp; // 临时队列用于保存未使用的优惠票
// 检查所有有效优惠票
while (!tickets.empty()) {
Ticket t = tickets.front(); // 获取队列首部的优惠票
tickets.pop(); // 从队列中移除
// 检查优惠票是否有效(时间差<=45分钟且公交票价<=地铁票价)
if (time - t.time <= 45 && price <= t.price) {
// 使用这张优惠票
used = true;
// 将队列中剩余的票转移到临时队列
while (!tickets.empty()) {
temp.push(tickets.front());
tickets.pop();
}
break; // 找到并使用一张优惠票后退出循环
} else {
// 当前优惠票无效,放入临时队列
temp.push(t);
}
}
// 将未使用的优惠票从临时队列放回主队列
while (!temp.empty()) {
tickets.push(temp.front());
temp.pop();
}
// 如果没有使用优惠票,则需要支付公交车费用
if (!used) {
total_cost += price;
}
}
}
// 输出总花费
cout << total_cost << endl;
return 0;
}
这里空空如也
有帮助,赞一个