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 @ 2024-10-24 12:12  Panda_LYL  阅读(32)  评论(0)    收藏  举报