正经题解
2025-08-05 20:59:30
发布于:河北
9阅读
0回复
0点赞
这道题的数据范围 1e5 内,一个循环暴力模拟就能过
再分析题目要求:
- 在搭乘一次地铁后可以获得一张优惠票,有效期为 45 分钟,在有效期内可以消耗这张优惠票。
- 免费搭乘一次票价不超过地铁票价的公交车。
代码如下
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, sum, cnt = 1;
int t[N], pri[N]; // 时间数组和价格数组
void xxx(int a[], int x) {
for (int i = x; i <= N; i ++) {
a[i] = a[i + 1];
if (a[i + 1] == 0) break;
}
}
int main() {
cin >> n;
while (n --) {
int opr, price, time;
cin >> opr >> price >> time;
if (opr == 0) { // 乘坐地铁
t[cnt] = time; // 时间
pri[cnt] = price; // 价格
cnt ++; // 优惠票的数量
sum += price;
}
else { // 乘坐公交
bool flag = false; // 判断是否能使用优惠券
for (int i = 1; i <= cnt; i ++) { // 在能选的优惠券中选择
if (time - t[i] <= 45 && price <= pri[i]) { // 判断条件
xxx(t, i); // 删除已用过的优惠券
xxx(pri, i); // 删除已用过的优惠券
flag = true; // 标记 flag
break;
}
}
if (!flag) sum += price;
}
}
cout << sum;
return 0;
}
可能会有遗漏,请大家多多包涵
这里空空如也
有帮助,赞一个