L2-034 口罩发放 (25 分)CPP实现

记一道很坑的L2题目。。调试了一晚上,一开始用vector存的个人口罩领取日期记录,结果测试点4、5一直超时。
在这里插入图片描述

后来改成map之后没有超时了,但是还是显示答案错误。
在这里插入图片描述

第二天上午又改了一个多小时。。最后发现是忽略了这一点

一开始是抱着侥幸心理,以为只比较用户提交时间不会影响列表出现顺序。最后还是老老实实加了个pid(优先级),最后AC。
在这里插入图片描述

AC代码如下:

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

class Person {
public:
	string name;
	string id;
	string time;
	int pid;	//优先级!!!

	Person(string name, string id, string time) {
		this->name = name;
		this->id = id;
		this->time = time;
	}

	bool operator==(const Person& a) {
		return a.id == this->id;
	}

};

bool checkId(string id) {
	if (id.size() != 18) {
		return true;
	}
	for (int j = 0; j < id.size(); j++)
	{
		if (id[j] > '9' || id[j] < '0') {
			return true;
		}
	}
	return false;
}


class Mycompare2 {
public:
	bool operator()(Person a, Person b) {
		if (a.time == b.time) {
			return a.pid < b.pid;
		}
		else {
			return a.time < b.time;
		}
	}
};


int main() {
	int D = 0, P = 0;
	cin >> D >> P;
	int T = 0, S = 0;
	map<string,int> allList;//总名单
	vector<Person> dayList;//日名单
	vector<Person> records;//身体不适成员
	int status;
	string id, time;
	string name;

	for (int i = 1; i <= D; i++)
	{
		dayList.clear();
		cin >> T >> S;
		for (int j = 0; j < T; j++)
		{
			cin >> name >> id >> status >> time;
			if (checkId(id) || name.size()>10) {
				continue;
			}
			Person person = Person(name, id,time);
			
			if (status == 1 && find(records.begin(), records.end(), person) == records.end()) {
				records.push_back(person);
			}
			person.pid = j;
			dayList.push_back(person);
		}		
		int count = 0;
		sort(dayList.begin(), dayList.end(), Mycompare2());
		for (vector<Person>::iterator it = dayList.begin(); it != dayList.end(); it++)
		{
			if (count == S) {
				break;
			}
			else {
				int day_record = allList[it->id];
				if (day_record != 0) {
					if (i - day_record < P + 1) {
						continue;
					}
					allList.erase(it->id);
				}
				allList[it->id]= i;
				cout << it->name << " " << it->id << endl;
				count++;
			}
		}
	}
	for (vector<Person>::iterator it = records.begin(); it != records.end(); it++)
	{
		cout << it->name << " " << it->id << endl;
	}
	return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值