P5661 [CSP-J2019] 公交换乘 题解

模拟

"公交换乘"按题意模拟即可.

注意:可以使用结构体,但是超过时间的优惠券需要被忽略.

代码

#include<iostream>
#include<cstdio>
using namespace std;
struct node{
int price, deadline, is_use;
// 价格,截止时间,是否使用过
}a[100005];
int n, p, ans, pos = 1;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int ex, price, t;
scanf("%d%d%d", &ex, &price, &t);
if (ex == 0) { // 地铁
a[++p].price = price; // 新的优惠券
a[p].deadline = t + 45; // 截止时间
ans += price; // 价格答案
}
else {
int flag = 0;
// 省略超时的优惠券
while (pos <= p && a[pos].deadline < t) pos++;
for (int j = pos; j <= p; j++) {
if (a[j].price >= price && !a[j].is_use) { // 价格到达要求,并且没有用过
a[j].is_use = 1; // 标记
flag = 1;
break;
}
}
// 没有适合的优惠券
if (!flag) ans += price;
}
}
printf("%d", ans);
return 0;
}
posted @   Panda_LYL  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示