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
思路
这题其实算是一个比较常规的排序题,但是时间卡得很紧(两个排序函数写出来基本上这题就能AC了,但是超时问题解决起来很麻烦……)。
题目大意就是给N行信息,每行有这个学生的准考证号和成绩。然后接下来是M行查询信息,其中第一个数字是查询的类型(这种方式和【1022 Digital Library (30分)】有点像),然后后面跟的是查询的信息。
- 如果查询的编号是1,那么后面跟的是PAT考试的等级,于是输出这个等级所有考生的信息(
准考证号
+成绩
),其中,按成绩降序排列,如果成绩相同,按准考证号升序排列; - 如果查询的编号是2,那么后面跟的是考场号,于是输出这个考场里
学生的总人数
+成绩总和
; - 如果查询的编号是3,那么后面跟的是日期,于是输出这个日期里
考场号
+这个考场的学生人数
,并且优先按学生人数降序排列,如果学生人数相同,那么按照考场号升序排列。
解决方法还是很常规的,就不多说了,这里说一下解决超时的问题:
- 关闭cin和cout的同步:
ios::sync_with_stdio(false);
; - 在排序函数cmp()中使用引用传参:
bool cmp(const info &a, const info &b)
(这个习惯很重要,我就是在这一直卡在最后一个测试点超时,全部改成引用之后就AC了); - 用
unordered_map
代替map
; - 用
scanf
和printf
代替cin
和cout
输出(但是有使用string的题目最好都用cin和cout,不然混合使用会显得很另类……); - 用
‘\n’
代替endl
。
代码
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<unordered_map>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 10010;
struct info{
string card_number;
string level;
string testSite_number;
string date;
string testee_number;
int score;
info(){
score = 0;
}
}m[maxn];
struct iinfo{
int testSite_number;
int Nt;
iinfo(){
Nt = 0;
}
};
bool cmp(const info &a, const info &b){
if(a.score!=b.score) return a.score>b.score;
else return a.card_number<b.card_number;
}
bool cmp2(const iinfo &a, const iinfo &b){
if(a.Nt!=b.Nt) return a.Nt>b.Nt;
else return a.testSite_number<b.testSite_number;
}
int main()
{
ios::sync_with_stdio(false);
int N, M;
cin>>N>>M;
cin.ignore();
for(int i=0;i<N;i++){
string tmpID;
int tmpScore;
cin>>tmpID>>tmpScore;
cin.ignore();
char tmpLevel = tmpID[0];
string tmpTestSite = tmpID.substr(1, 3);
string tmpDate = tmpID.substr(4, 6);
string tmpTesteeNumber = tmpID.substr(10, 3);
m[i].card_number = tmpID;
m[i].level = tmpLevel;
m[i].testSite_number = tmpTestSite;
m[i].date = tmpDate;
m[i].testee_number = tmpTesteeNumber;
m[i].score = tmpScore;
}
for(int i=1;i<=M;i++){
string tmpQuery;
getline(cin, tmpQuery);
char type = tmpQuery[0];
string tmpInfo = tmpQuery.substr(2);
bool flag = false;
cout<<"Case "<<i<<": "<<tmpQuery<<'\n';
if(type=='1'){
vector<info> List;
for(int j=0;j<N;j++){
if(m[j].level==tmpInfo){
List.push_back(m[j]);
flag = true;
}
}
if(flag==true){
sort(List.begin(), List.end(), cmp);
for(int j=0;j<List.size();j++){
cout<<List[j].card_number<<" "<<List[j].score<<'\n';
}
}
else cout<<"NA\n";
}
else if(type=='2'){
int Nt = 0, Ns = 0;
for(int j=0;j<N;j++){
if(m[j].testSite_number==tmpInfo){
Nt++;
Ns += m[j].score;
flag = true;
}
}
if(flag==true){
cout<<Nt<<" "<<Ns<<'\n';
}
else cout<<"NA\n";
}
else if(type=='3'){
unordered_map<int, int> site;
vector<iinfo> List;
for(int j=0;j<N;j++){
if(m[j].date==tmpInfo){
site[stoi(m[j].testSite_number)]++;
flag = true;
}
}
if(flag==true){
for(unordered_map<int, int>::iterator itt=site.begin();itt!=site.end();itt++){
iinfo tmp;
tmp.testSite_number = itt->first;
tmp.Nt = itt->second;
List.push_back(tmp);
}
sort(List.begin(), List.end(), cmp2);
for(int j=0;j<List.size();j++){
cout<<List[j].testSite_number<<" "<<List[j].Nt<<'\n';
}
}
else cout<<"NA\n";
}
}
return 0;
}