1085 PAT單位排行 (25 分)
每次 PAT 考試結束後,考試中心都會發佈一個考生單位排行榜。本題就請你實現這個功能。(超時小霸王)
#include<iostream> #include<string> #include<map> #include<set> #include<algorithm> using namespace std; struct school { string id; set<string> student; double sum; bool operator <( const school& b) const { if ((int)sum != (int)b.sum) return (int)sum > (int)b.sum; else if (student.size() != b.student.size()) return student.size()< b.student.size(); else return id < b.id; } }; map<char, double> tran = { make_pair('B', 2.0/3),make_pair('A', 1) ,make_pair('T',1.5) }; map<string, school> myschool; set<school> result; int main(){ int N; cin >> N; string student_id, nm; int tmp; for (int i = 0; i < N; i++) { cin >> student_id >> tmp >> nm; transform(nm.begin(), nm.end(), nm.begin(), ::tolower); myschool[nm].id = nm; myschool[nm].student.insert(student_id.substr(1, string::npos)); myschool[nm].sum += tran[student_id[0]]*tmp; } for (auto it = myschool.begin(); it != myschool.end(); it++) result.insert(it->second); int ranking=0, count = 0; int pre=-1; cout << result.size() << endl; for (auto it = result.begin(); it != result.end(); it++) { count++; if ((int)it->sum != pre) ranking = count; pre = (int)it->sum; cout << ranking << " " << it->id << " " << (int)it->sum << ' ' << it->student.size() << endl; } return 0; }