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 N (≤104) 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:
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
Ω
做这种题目的时候总是心惊胆战,生怕哪个地方优化没做好,数据结构没搞好就TLE。
给出若干个PAT考试登记码: ,接下来会有多个查询,有三种查询模式:
Type 1:输入level(B、A、T),按照成绩降序输出所有参加这个等级考的学生登记码和成绩,成绩一样按登记码升序排列
Type 2:输入考场号,输出在该考场参加考试的总人数以及总成绩
Type 3:输入日期,按照当日考试人数降序输出所有有人考试的考场号,以及相应的考试人数,如果考试人数一致则按考场号升序排列
查询结果为空就输出NA。
前两种查询方式都挺好处理。读取完所有登记码和成绩,对三个level的信息分别进行排序;注意到考场号只有三位数,那么直接开一个vector<pair<int,int>> site(1000)
,考场号即为索引,来存储人数和总成绩。第三个查询模式就比较棘手了,我的思路是直接开一个map<int,map<int,int>>
来存储单个日期下单个考场的人数,在查询时把子map拷贝到一个vector中,然后再进行排序输出。
第一次提交,后三个测试点全是TLE,把我看傻了,心态直接崩。不知道还有什么优化手段,于是去网上看了些题解,发现暴力循环遍历都能过,百思不得其解。我试着把三个level的排序部分给注释掉,竟然发现时间骤减。仔细研究一番,发现问题出在lambda表达式中的传参。由于我把登记码和成绩单独存储在一个vector<pair<int,int>> info
中,而三个level都只存info的索引,因此在sort的lambda函数中需要将info传入:
[info](int a, int b) {return info[a].second == info[b].second ? info[a].first < info[b].first : info[a].second > info[b].second;}
那么就意味着每次调用lambda函数时都会拷贝一遍info,从而造成了大量的时间开销。其实lambda函数是可以传入引用的,只要在变量名前加上&即可:
[&info](int a, int b)
传入所有参数是[=]
,⚠️这也是拷贝传参。如果需要传入多个参数就用【,】分隔。
🐎
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#define idx(x) (x=='B'?0:x=='A'?1:2)
using namespace std;
typedef const pair<int, int> cpii;
int main()
{
int n, m, tp, s, d;
scanf("%d %d", &n, &m);
vector<pair<string, int>> info(n, {string(13, 0), 0});
vector<pair<int, int>> site(1000);
vector<int> level[3];
unordered_map<int, unordered_map<int, int>> date;
for (int i = 0; i < n; ++i)
{
scanf("%s %d", &info[i].first[0], &info[i].second);
s = stoi(info[i].first.substr(1, 3)), d = stoi(info[i].first.substr(4, 6));
level[idx(info[i].first[0])].push_back(i);
++site[s].first; site[s].second += info[i].second;
++date[d][s];
}
for (auto &k: level)
sort(k.begin(), k.end(), [&info](int a, int b)
{return info[a].second == info[b].second ? info[a].first < info[b].first : info[a].second > info[b].second;});
string st;
for (int i = 0; i < m; ++i)
{
cin >> tp >> st;
printf("Case %d: %d %s\n", i + 1, tp, st.c_str());
switch (tp)
{
case 1:
if (level[idx(st[0])].empty()) printf("NA\n");
else
for (auto &k: level[idx(st[0])])
printf("%s %d\n", info[k].first.c_str(), info[k].second);
break;
case 2:
if (!site[stoi(st)].first) printf("NA\n");
else printf("%d %d\n", site[stoi(st)].first, site[stoi(st)].second);
break;
case 3:
if (date[stoi(st)].empty()) printf("NA\n");
else
{
vector<pair<int, int>> tmp(date[stoi(st)].begin(), date[stoi(st)].end());
sort(tmp.begin(), tmp.end(), [](cpii &a, cpii &b)
{return a.second == b.second ? a.first < b.first : a.second > b.second;});
for (auto &k: tmp)
printf("%03d %d\n", k.first, k.second);
}
break;
}
}
}