思路:边读入数据边处理,安题干要求分类保存。查询时直接输出。
第三类按日期查询时数据不好保存,还是按y总的方法查询时处理更简单。
C++ 代码
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
const int S = 1010;
struct Node
{
string id;
int score;
};
struct Site
{
int cnt, sum;
Site() {
cnt = 0;
sum = 0;
}
}site[S];
bool cmp(Node a, Node b) {
if (a.score != b.score) return a.score > b.score;
else return a.id < b.id;
}
vector<Node> la, lb, lt;
unordered_map<string, unordered_map<string, int>> dateHash;
int main() {
int n, q;
cin >> n >> q;
for (int i = 0; i < n; i++)
{
Node t;
cin >> t.id >> t.score;
if (t.id[0] == 'A') la.push_back(t);
if (t.id[0] == 'B') lb.push_back(t);
if (t.id[0] == 'T') lt.push_back(t);
string s = t.id.substr(1, 3);
site[stoi(s)].cnt++;
site[stoi(s)].sum += t.score;
string date = t.id.substr(4, 6);
dateHash[date][s]++;
}
sort(la.begin(), la.end(), cmp);
sort(lb.begin(), lb.end(), cmp);
sort(lt.begin(), lt.end(), cmp);
for (int i = 1; i <= q; i++)
{
string t, c;
cin >> t >> c;
printf("Case %d: %s %s\n", i, t.c_str(), c.c_str());
if (t == "1")
{
if (c[0] == 'A') {
if (la.empty()) puts("NA");
else
for (auto it : la) printf("%s %d\n", it.id.c_str(), it.score);
}
if (c[0] == 'B') {
if (lb.empty()) puts("NA");
else
for (auto it : lb) printf("%s %d\n", it.id.c_str(), it.score);
}
if (c[0] == 'T') {
if (lt.empty()) puts("NA");
else
for (auto it : lt) printf("%s %d\n", it.id.c_str(), it.score);
}
}
else if (t == "2")
{
int idx = stoi(c);
if (site[idx].cnt == 0) puts("NA");
else printf("%d %d\n", site[idx].cnt, site[idx].sum);
}
else
{
vector<pair<int, string>> rooms;
for (auto item : dateHash[c]) rooms.push_back({ -item.second, item.first });
sort(rooms.begin(), rooms.end());
if (rooms.empty()) puts("NA");
else
for (auto room : rooms)
printf("%s %d\n", room.second.c_str(), -room.first);
}
}
return 0;
}