题目解析
给定n天申请口罩的信息。求每一天申报成功的人员信息,以及最后有异常状态的人员信息
能发口罩的
- 身份证 必须为 18位 的数字
- 同一身份证必须隔 P 天才能再次申请
- 在上面两条都符合的情况下,按照提交时间的先后顺序发放,直至全部记录处理完毕或 个名额用完。如果提交时间相同,则按照在列表中出现的先后顺序决定。
解题思路
按照题意模拟即可
可以先按照输入的内容先统计身体状态有异常的人
输出按时间以及编号排序,且在名额内的申报口罩人员信息
最后输出身体状态有异常的人员信息
注: java 4、5 数据TLE
代码
import java.io.*;
import java.util.*;
public class Main
{
// 判断身份证是否为18位,且都是数字
static boolean check(String S)
{
int len = S.length();
if (len != 18) return false;
char s[] = S.toCharArray();
for (int i = 0; i < len; i++)
{
if (!('0' <= s[i] && s[i] <= '9')) return false;
}
return true;
}
public static void main(String[] args)
{
int d = sc.nextInt(), p = sc.nextInt();
ArrayList<edge> yc = new ArrayList<edge>(); // 身体状态有异常的
TreeMap<String, Integer> last = new TreeMap<String, Integer>(); // 上一次申请的时间
for (int T = 1; T <= d; T++)
{
int n = sc.nextInt(), m = sc.nextInt();
edge shu[] = new edge[n + 10];
for (int i = 1; i <= n; i++)
{
shu[i] = new edge();
shu[i].name = sc.next(); // 姓名
shu[i].idname = sc.next(); // 身份证
shu[i].op = sc.nextInt(); // 身体状态
shu[i].time = sc.next(); // 申报时间
shu[i].id = i; // 编号
// 如果身份证符合 且 身体状态有异常的
if (check(shu[i].idname) && shu[i].op == 1) yc.add(shu[i]);
}
Arrays.sort(shu, 1, n + 1);
for (int i = 1; i <= n; i++)
{
if (m == 0) break; // m个名额满了
String idnum = shu[i].idname; // 当前人员的身份证
if (check(idnum)) // 判断身份证是否合法
{
// 如果之前没有申报过 或者 当前离上次申报时间超过p天
if (!last.containsKey(idnum) || T - last.get(idnum) > p)
{
last.put(idnum, T); //更新申报时间
m--; // 名额减少
out.println(shu[i]);
}
}
}
}
TreeSet<String> trS = new TreeSet<String>(); // 判断是否输出过
for (edge i : yc)
{
if (trS.contains(i.idname)) continue;
out.println(i);
trS.add(i.idname);
}
out.flush();
out.close();
}
static class edge implements Comparable<edge>
{
String name, idname, time;
int op, id;
public edge()
{
}
public String toString()
{
return name + " " + idname;
}
@Override
public int compareTo(edge other)
{
if (time != other.time)
return time.compareTo(other.time);
return id - other.id;
}
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}
c++
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
const int N = 1e3 + 10;
struct edge
{
string name, idnum, time;
int op, id;
} shu[N];
unordered_map<string, int> last;
vector<edge> yc;
bool check(string s)
{
int len = s.size();
if(len != 18) return false;
for(int i = 0; i < len; i ++)
{
if('0' > s[i] || s[i] > '9') return false;
}
return true;
}
bool cmp(edge a, edge b)
{
if(a.time != b.time) return a.time < b.time;
return a.id < b.id;
}
int main()
{
int d, p; cin >> d >> p;
for(int T = 1; T <= d; T ++)
{
int n, m; cin >> n >> m;
for(int i = 1; i <= n; i ++)
{
cin >> shu[i].name >> shu[i].idnum >> shu[i].op >> shu[i].time;
shu[i].id = i;
if(check(shu[i].idnum) && shu[i].op == 1)
yc.push_back(shu[i]);
}
sort(shu + 1, shu + n + 1, cmp);
vector<edge> res;
for(int i = 1; i <= n; i ++)
{
if(m == 0) break;
string idnum = shu[i].idnum;
if(check(idnum))
{
if(last[idnum] == 0 || T - last[idnum] > p)
{
last[idnum] = T;
m --;
res.push_back(shu[i]);
}
}
}
for(auto i : res)
cout << i.name << " " << i.idnum << endl;
}
unordered_set<string> st;
for(auto i : yc)
{
if(st.count(i.idnum) > 0) continue;
cout << i.name << " " << i.idnum << endl;
st.insert(i.idnum);
}
return 0;
}