PAT甲级 1153. Decode Registration Card of PAT

前言

这是第二道题25分类型,这个题文字有点多,一定要细心看清题目的要求。这类模拟题难就难在信息量多,容易忽略很多小细节,算法上没有难处。不过我在这题犯了一些低级错误,还是题目量不够啊。

题目原文

A registration card number of PAT consists of 4 parts:

the 1st letter represents the test level, namely, T for the top level, A for advance and B for basic;
the 2nd - 4th digits are the test site number, ranged from 101 to 999;
the 5th - 10th digits give the test date, in the form of yymmdd;
finally the 11th - 13th digits are the testee’s number, ranged from 000 to 999.
Now given a set of registration card numbers and the scores of the card owners, you are supposed to output the various statistics according to the given queries.

Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (≤10^4) and M (≤100), the numbers of cards and the queries, respectively.

Then N lines follow, each gives a card number and the owner’s score (integer in [0, 100]), separated by a space.

After the info of testees, there are M lines, each gives a query in the format Type Term, where

Type being 1 means to output all the testees on a given level, in non-increasing order of their scores. The corresponding Term will be the letter which specifies the level;
Type being 2 means to output the total number of testees together with their total scores in a given site. The corresponding Term will then be the site number;
Type being 3 means to output the total number of testees of every site for a given test date. The corresponding Term will then be the date, given in the same format as in the registration card.
Output Specification:
For each query, first print in a line Case #: input, where # is the index of the query case, starting from 1; and input is a copy of the corresponding input query. Then output as requested:

for a type 1 query, the output format is the same as in input, that is, CardNumber Score. If there is a tie of the scores, output in increasing alphabetical order of their card numbers (uniqueness of the card numbers is guaranteed);
for a type 2 query, output in the format Nt Ns where Nt is the total number of testees and Ns is their total score;
for a type 3 query, output in the format Site Nt where Site is the site number and Nt is the total number of testees at Site. The output must be in non-increasing order of Nt’s, or in increasing order of site numbers if there is a tie of Nt.
If the result of a query is empty, simply print NA.

Sample Input:
8 4
B123180908127 99
B102180908003 86
A112180318002 98
T107150310127 62
A107180908108 100
T123180908010 78
B112160918035 88
A107180908021 98
1 A
2 107
3 180908
2 999

Sample Output:
Case 1: 1 A
A107180908108 100
A107180908021 98
A112180318002 98
Case 2: 2 107
3 260
Case 3: 3 180908
107 2
123 2
102 1
Case 4: 2 999
NA

解决思路

首先根据每个考生建立一个结构体,两个成员,分别是ID和分数。然后设置一个存储所有考生的vector,我因为习惯用指针来存储结构体,所以我这个vector存的是指针类型。

然后根据三类不同的查询写三个不同的函数。

第一类把符合查询要求的考生单独建立一个子集,然后根据要求排序输出即可,没有的情况下就输出NA。

第二类设置两个变量分别记录符合要求的学生总数和其分数总和,然后遍历所有考生,按条件对两个变量进行增量。如果学生人数为零,则输出NA。

第三类则用一个map存储 地点-人数 对,但是由于有一个case超时无法通过,在参考柳诺的博客之后,了解到map是红黑树实现的,unordered_map是散列表实现的,意味着后者的查询效率要高很多。所以将map替换为unordered_map。然后将其转换为vector按题目要求进行排序即可。然后按序输出,如果map是空的就输出NA。

源代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

struct Testee {
string ID;
int score;
Testee(string id, int s) : ID(id), score(s) {}
};

vector<Testee *> testees;

bool compareByScore(Testee * const &a, Testee * const &b) {
if(a->score == b->score) {
return a->ID < b->ID;
} else {
return a->score > b->score;
}
}

void queryOne(string level) {
vector<Testee *> subTestees;
for(int i = 0; i < testees.size(); i++) {
if((testees[i]->ID).substr(0, 1) == level) {
subTestees.push_back(testees[i]);
}
}
if(subTestees.empty()) {
printf("NA\n");
} else {
sort(subTestees.begin(), subTestees.end(), compareByScore);
for(int i = 0 ; i < subTestees.size(); i++) {
printf("%s %d\n", subTestees[i]->ID.c_str(), subTestees[i]->score);
}
}
}

void queryTwo(string site) {
int Nt = 0, Ns = 0;
for(int i = 0; i < testees.size(); i++) {
if((testees[i]->ID).substr(1, 3) == site) {
Nt++;
Ns += testees[i]->score;
}
}
if(Nt == 0) {
printf("NA\n");
} else {
printf("%d %d\n", Nt, Ns);
}
}

bool compareByNt(pair<string, int> const &a, pair<string, int> const &b) {
if(a.second == b.second) {
return a.first < b.first;
} else {
return a.second > b.second;
}
}

void queryThree(string date) {
unordered_map<string, int> siteNtMap;
for(int i = 0; i < testees.size(); i++) {
if((testees[i]->ID).substr(4, 6) == date) {
siteNtMap[(testees[i]->ID).substr(1, 3)]++;
}
}
if(siteNtMap.empty()) {
printf("NA\n");
} else {
vector<pair<string, int> > siteNtVec(siteNtMap.begin(), siteNtMap.end());
sort(siteNtVec.begin(), siteNtVec.end(), compareByNt);
for(int i = 0; i < siteNtVec.size(); i++) {
printf("%s %d\n", siteNtVec[i].first.c_str(), siteNtVec[i].second);
}
}
}

int main(void) {
int N, M;
cin >> N >> M;

string ID;
int score;
for(int i = 0; i < N; i++) {
cin >> ID >> score;
Testee *testee = new Testee(ID, score);
testees.push_back(testee);
}

int type;
string term;
for(int i = 0; i < M; i++) {
cin >> type >> term;
printf("Case %d: %d %s\n", i + 1, type, term.c_str());
if(type == 1) {
queryOne(term);
} else if(type == 2) {
queryTwo(term);
} else {
queryThree(term);
}
}
return 0;
}

后记

除了一开始的时候输出格式上有一些细节问题外,还有就是用map导致的超时,应该改为unordered_map。但是这些都很快解决了,最傻的是我犯了一个低级错误导致耽误了很长时间,就是第三个函数里的那个unordered_map我一开始是定义在函数外的,也就是全局范围,这会导致之后如果还有第二次查询第三类的话,就数据重叠了。所以说,还是题目做少了,才会犯这种低级错误,一定要多刷题。