这道题一开始一直超时,后来看了大神的发现要用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"); };
原文地址:https://www.cnblogs.com/wsggb123/p/10206062.html
时间: 2024-10-10 11:13:57