题目描述
输入样例
4 2
5 3
A 123456789012345670 1 13:58
B 123456789012345671 0 13:58
C 12345678901234567 0 13:22
D 123456789012345672 0 03:24
C 123456789012345673 0 13:59
4 3
A 123456789012345670 1 13:58
E 123456789012345674 0 13:59
C 123456789012345673 0 13:59
F F 0 14:00
1 3
E 123456789012345674 1 13:58
1 1
A 123456789012345670 0 14:11
输出样例
D 123456789012345672
A 123456789012345670
B 123456789012345671
E 123456789012345674
C 123456789012345673
A 123456789012345670
A 123456789012345670
E 123456789012345674
思路:模拟
每次输入一个人的信息
1.首先判断其身份证号是否有效,如果有,先将其视为可以领到口罩的人,加入本次的p数组,同时需要判断其身体状况是否为1,是否出现过,如果符合上述两种情况,加入ans数组
2.对当天的p数组进行排序
3.遍历排序好的p数组,判断每个人是否有获得口罩的资格,如果有,将其加入res数组
4.输出结果
注意点
1.身份证号是唯一凭证,统一身份证号一定对应同一个名字,但同一个名字不一定对应同一个身份证号
2.判断间隔天数时,判断条件应为当天 - 上一次领取时间 > 间隔时间,而不是当天 - 上一次领取时间 == 间隔时间 + 1, 因为有可能间隔好多天才再次领取
AC代码
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int N = 1010;
typedef struct Person
{
string name; //姓名
int application_time; //申请时间
int idx; //申请先后
}person;
person p[N];
vector<person> res; //可以领到口罩的人
vector<person> ans; //合法记录的、身体状况为 1 的申请人
map<string,int> start; //记录每个人申请口罩的起始时间
map<string,int> flag1, flag2; //flag1记录有资格领取口罩的人是否出现过 ,flag2标记有合法记录的、身体状况为 1 的申请人是否出现过
map<string, string> mp; //姓名对应身份证号
int D, P; //共有D天,间隔P天
bool cmp(person p1, person p2)
{
if(p1.application_time == p2.application_time) return p1.idx < p2.idx;
return p1.application_time < p2.application_time;
}
bool Judge_Id(string id) //判断身份证是否合法
{
if(id.size() != 18) return false;
for(int i = 0; id[i]; i ++)
{
if(id[i] < '0' || id[i] > '9') return false;
}
return true;
}
bool Judge(string id, int now) //判断是否有资格获得口罩
{
if(!flag1[id]) //首先,再当天身份证号为id的人没有领过口罩
{
if(!start[id] || (now - start[id] > P)) return true; //第一次取口罩 或 //以前取过口罩,但已经间隔P天
return false;
}
return false;
}
int main()
{
cin >> D >> P;
for(int i = 1; i <= D; i ++)
{
int t, s; //t条申请,s个名额
int pos = 0; //当天有效人数
flag1.clear(); //标记当天有资格领取口罩的人是否重复领取
cin >> t >> s;
for(int j = 0; j < t; j ++)
{
string name, id;
int state, st, ed;
char c;
cin >> name >> id >> state >> st >> c >> ed;
if(Judge_Id(id)) //本人有效
{
mp[name] = id;
p[pos] = {name, st * 60 + ed, j};
if(state == 1 && !flag2[id]) // 合法记录的、身体状况为 1 的申请人
{
flag2[id] = 1;
ans.push_back(p[pos]);
}
pos ++;
}
}
sort(p, p + pos, cmp);
for(int j = 0; j < pos && s; j ++)
{
string name = p[j].name;
string id = mp[name];
if(Judge(id, i))
{
flag1[id] = 1;
start[id] = i;
res.push_back(p[j]);
s --;
}
}
// cout << "-----" << endl;
// for(int j = 0; j < res.size(); j ++) cout << res[j].name << " " << mp[res[j].name] << " " << start[mp[res[j].name]] << endl;
}
//cout << "-----" << endl;
for(int i = 0; i < res.size(); i ++) cout << res[i].name << " " << mp[res[i].name] << endl;
//cout << "---------" << endl;
for(int i = 0; i < ans.size(); i ++) cout << ans[i].name << " " << mp[ans[i].name] << endl;
return 0;
}
欢迎大家批评指正!!!