题目
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 andB
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 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 correspondingTerm
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 correspondingTerm
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 correspondingTerm
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
whereNt
is the total number of testees andNs
is their total score; - for a type 3 query, output in the format
Site Nt
whereSite
is the site number andNt
is the total number of testees atSite
. The output must be in non-increasing order ofNt
's, or in increasing order of site numbers if there is a tie ofNt
.
If the result of a query is empty, simply print NA
.
Sample Input:
1 | 8 4 |
Sample Output:
1 | Case 1: 1 A |
题解
思路
- 这道题在网上看到暴力解也能过,不过为了更优雅地AC以及应对更大的数据集测试量,这里放的是优雅的解法。
- 首先题目的意思是给你一堆学生的信息,包含
- 科目
- 考场
- 时间
- 成绩
- 编号
- 然后会向你查询
- 查询的内容有
- 给定科目,输出参加这门考试的所有学生的总编号和成绩,成绩降序+编号升序
- 给定考场,输出这个考场的总人数和总分数
- 给定考试时间,输出这个时间的考场和当天的考场人数,按照人数降序+考场升序
- 应对查询问题,最好的方法就是哈希表,所以这题难在数据结构的设计,尤其是对于第三个。
数据结构
- level 是一个三项的列表,这三项对应级别B,A,和T
- 其中的任意一项,存放考这个级别考试的所有考生
- 一个考生有编号和成绩两项
- 对于Python来说,直接[B122,21]表示一个学生,对于C++,使用了一个struct,为了方便排序。C++对于一个level里,存的是一个set,因为可以边插入边排序,降低复杂度
- 其中的任意一项,存放考这个级别考试的所有考生
- 对于第二个查询,site而言,python使用了一个列表,而C++使用了site_num[]和site_score[]两个列表。
- Python用[人数,分数]来表示一个考成
- C++ 就是下标和值相对应的两个列表
- 第二个查询比较简单,不涉及排序
- date 是最难的。date是一个哈希集,将时间映射到另一个哈希集。另一个哈希集将教室映射到人数。
- 比如date[970331]是存放97年3月31日的所有考场信息。
- 一个考场信息也是一个哈希集,代表人数,比如
date[970331][103] = 3
代表在那一天,103教室的所有人数是3。对于Python和C++都是这个设计。
- 以上方的数据结构,我们所有的查询都可以尽量缩短至
O(1)
。除了最后一个date,需要在查询的时候排序。这也是没办法的事,因为我们的排序依据只有查询完了才知道。
算法
- 读入数据
- 更新相应的level
- 更新相应的site
- 更新相应的date
- 读入查询
- 按照情况输出即可
- 注意NA的情况
代码
- 这道题使用PYTHON会有两个点超时,因此放了两种语言的解。
- 对于C++来说,有些查询结果是一旦插入就确定好顺序的,比如查询科目的时候。这时候使用了set这种类型,可以保证插入的时候就是有序的。像对于一个时间的所有考场,则必须等到输入完所有的数据才能排序,因为考场人数是最后才能确定的。
Python
1 | from collections import defaultdict |
C++
1 |
|