返回题解分享
讨论 / 题解分享/ 帖子详情

外卖店优先级(编程题) - 题解

蓝桥OJ全过

#include <bits/stdc++.h>
using namespace std;
// #define int long long
// #define LL long long
#define endl '\n'

const int N = 1e5 + 10;

int n, M, T; // n家外卖店,M条订单信息,T个时刻
vector<int> record[N]; // 订单信息:每条表示ts时刻编号id的外卖店收到一个订单
int shop[N]; // 商店优先级,初始优先级为0
bool st[N]; // 某时刻该店是否有订单
set<int> cache; // 优先缓存

void solve()
{
	cin >> n >> M >> T;
	for (int i = 1; i <= M; i++)
	{
		int ts, id;
		cin >> ts >> id;
		record[ts].push_back(id); // ts时刻编号id的外卖店收到一个订单
	}

	for (int t = 1; t <= T; t++) // 每个时刻发生的事件
	{
		for (int id : record[t]) // 处理订单信息
		{
			shop[id] += 2; // 每有一单优先级+2
			st[id] = true; // 该时刻有订单
		}

		for (int id = 1; id <= n; id++) // 每个商店在该时刻发生的事件
		{
			if (!st[id]) // 该时刻没有订单 
				shop[id] = max(0, shop[id] - 1); // 优先级-1,最低减到0
			if (shop[id] > 5) // 该时刻优先级大于5
				cache.insert(id); // 加入优先缓存
			if (shop[id] <= 3) // 该时刻优先级小于等于5
				cache.erase(id); // 踢出优先缓存
		}
		memset(st, false, sizeof st); // 以免使用了上一个时刻的订单信息
	}

	cout << cache.size() << endl; // 最后时刻有多少外卖店在优先缓存内即cache的大小
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	solve();

	return 0;
}
0 回复 0 转发 0 喜欢 0 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!