L2-034 口罩发放

题目 L2-034 口罩发放

image-d4ac769e

思路分析

代码实现

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'

#define int long long

using ll = long long;

using ull = unsigned long long;

using PII = pair<int, int>;

using Pll = pair<ll, ll>;

int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };

const int inf = 0x3f3f3f3f;

struct People {

	string name;

	string id;

	int bodyState;

	string time;

	int sx;

	bool operator<(const People& rhs) const {

		if(time!=rhs.time)

			return time<rhs.time;

		return sx<rhs.sx;

	}

};

bool isValidID(const string &id) {

	if (id.size() != 18) return false;

	for (char c : id) {

		if (!isdigit(c)) return false;

	}

	return true;

}

signed main() {

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

	int d,p;

	cin>>d>>p;

	map<string, int> lastDay;      // 记录每人上一次领取口罩的日期

	map<string, bool> sickLogged;  // 身体有症状的人员是否已记录

	vector<People> illPeople;      // 身体不适人员名单

	for (int day = 1; day <= d; ++day) {

		int t,s;//第?天 t个申请 s个名额

		cin>>t>>s;

		vector<People> applicants(t);

		for(int i=0; i<t; i++) {

			cin >> applicants[i].name >> applicants[i].id >> applicants[i].bodyState >> applicants[i].time;

			applicants[i].sx = i; // 输入顺序

		}

		// 记录身体不适的合法人员

		for(int i=0; i<t; i++) {

			const auto& person = applicants[i];

			if (!isValidID(person.id)) continue;

			if (person.bodyState == 1 && !sickLogged[person.id]) {

				sickLogged[person.id] = true;

				illPeople.push_back(person);

			}

		}

		sort(applicants.begin(),applicants.end());

		int given = 0; // 已发放数

		for (const auto& person : applicants) {

			if (!isValidID(person.id)) continue;

			// 如果发放完了,就跳出

			if (given >= s) break;

			// 检查间隔天数限制

			if (lastDay.count(person.id) == 0 || lastDay[person.id] + p < day) {

				cout << person.name << " " << person.id << '\n';

				lastDay[person.id] = day;

				++given;

			}

		}

	}

	for (const auto& person : illPeople) {

		cout << person.name << " " << person.id << '\n';

	}

	return 0;

}

同类题型

视频讲解


⬅️ L2-033 简单计算器 🏠 00-天梯赛 ➡️ L2-035 完全二叉树的层序遍历