1153 Decode Registration Card of PAT (25分)
生活随笔
收集整理的這篇文章主要介紹了
1153 Decode Registration Card of PAT (25分)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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; }總結
以上是生活随笔為你收集整理的1153 Decode Registration Card of PAT (25分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 四时田园杂兴其三十一的诗意
- 下一篇: PAT 1152 Google Recr