1. 程式人生 > >1153 Decode Registration Card of PAT

1153 Decode Registration Card of PAT

這道題一開始一直超時,後來看了大神的發現要用unordered_map,然後還是不行,把cout改成printf,居然過了。。。時間真的掐得緊

#include <iostream>
#include <stdlib.h>
#include <string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>

#include <unordered_map>

using namespace std;

struct
stu { string cnum; int score; }; struct site { int num; string s; }; bool cmp1(stu x, stu y) { if (x.score != y.score) { return x.score > y.score; } else { return x.cnum < y.cnum; } } bool cmp2(site x, site y) { if (x.num != y.num) {
return x.num > y.num; } else { return x.s < y.s; } } int main(){ int m, n; cin >> n >> m; vector<stu> arr; for (int i = 0; i < n; i++) { stu s; cin >> s.cnum >> s.score; arr.push_back(s); }
for (int i = 0; i < m; i++) { int type; cin >> type; if (type == 1) { char level; cin >> level; int num = 0; sort(arr.begin(), arr.end(), cmp1); cout << "Case" << ' ' << i + 1 << ": " << type << ' ' << level<<endl; for (int j = 0; j < arr.size(); j++) { if (arr[j].cnum[0] == level) { printf("%s %d\n", arr[j].cnum.c_str(), arr[j].score); num++; } } if (num == 0) { cout << "NA" << endl; } } if (type == 2) { string site; cin >> site; int num = 0, total = 0; for (int j = 0; j < arr.size(); j++) { if (arr[j].cnum.substr(1, 3) == site) { num++; total += arr[j].score; } } cout << "Case" << ' ' << i + 1 << ": " << type << ' ' << site<<endl; if (num == 0) { cout << "NA" << endl; } else { cout << num << ' ' << total << endl; } } if (type == 3) { string date; cin >> date; unordered_map<string, int> m1; vector<site> arr1; for (int j = 0; j < arr.size(); j++) { if (arr[j].cnum.substr(4, 6) == date) { m1[arr[j].cnum.substr(1, 3)]++; } } for (auto it : m1) { site temp; temp.s = it.first; temp.num = it.second; arr1.push_back(temp); } sort(arr1.begin(), arr1.end(), cmp2); cout << "Case" << ' ' << i + 1 << ": " << type << ' ' << date << endl; if (arr1.size() == 0) { cout << "NA" << endl; } else { for (int j = 0; j < arr1.size(); j++) { printf("%s %d\n", arr1[j].s.c_str(), arr1[j].num); } } } } system("pause"); };