1153 Decode Registration Card of PAT (25分)

tech2022-10-17  99

1153 Decode Registration Card of PAT (25分)

题意:

给出一组学生的准考证号和成绩 准考证号组成为:考试等级(TAB),考场号(从101到999),考试日期(yymmdd),考生号(000到999) 有三种查询方式: 第一种:给出考试等级,找到该等级的考生,然后按照成绩降序,准考证升序 排列 第二种:给出考场号,给出该考场的考生数量和总得分 第三种:给定考试日期,查询该日期下所有每个考场对应的考试人数,按照人数降序,考场号升序排列

题解:

前两个查询暴力找就行 第三个用按照日期查询每个考场人数,用unordered_map存储,最后排序 用map会超时,unordered_map不会

map与unordered_map区别 map内元素排列有序,unordered_map内无序 unorder_map占用的内存要高。 但是unordered_map执行效率要比map高很多

代码:

#include <iostream> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; struct node { string t; int value; }; bool cmp(const node &a, const node &b) { return a.value != b.value ? a.value > b.value : a.t < b.t; } int main() { int n, k, num; string s; cin >> n >> k; vector<node> v(n); for (int i = 0; i < n; i++) cin >> v[i].t >> v[i].value; for (int i = 1; i <= k; i++) { cin >> num >> s; printf("Case %d: %d %s\n", i, num, s.c_str()); vector<node> ans; int cnt = 0, sum = 0; if (num == 1) { for (int j = 0; j < n; j++) if (v[j].t[0] == s[0]) ans.push_back(v[j]); } else if (num == 2) { for (int j = 0; j < n; j++) { if (v[j].t.substr(1, 3) == s) { cnt++; sum += v[j].value; } } if (cnt != 0) printf("%d %d\n", cnt, sum); } else if (num == 3) { unordered_map<string, int> m; for (int j = 0; j < n; j++) if (v[j].t.substr(4, 6) == s) m[v[j].t.substr(1, 3)]++; for (auto it : m) ans.push_back({it.first, it.second}); } sort(ans.begin(), ans.end(),cmp); for (int j = 0; j < ans.size(); j++) printf("%s %d\n", ans[j].t.c_str(), ans[j].value); if (((num == 1 || num == 3) && ans.size() == 0) || (num == 2 && cnt == 0)) printf("NA\n"); } return 0; }
最新回复(0)