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
题意
略
思路
题目本身不复杂,就是要的三种查询比较繁琐,建立适当的结构体统计排序再根据查询输出即可。
代码
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <cstring>
#include <algorithm>
#define MAX_N 100000
#define MAX_SITE 1000
using namespace std;
// 记录及其排序
struct Record{
string card;
int score;
Record(string card, int score) : card(card), score(score){};
};
bool recordCmp(Record& r1, Record& r2){
return r1.score != r2.score ? r1.score > r2.score : r1.card < r2.card;
}
// 考场及映射map
struct Site{
int cnt;
int total;
Site(): cnt(0), total(0){};
}sites[MAX_SITE];
int siteCnt = 0;
map<int, int> siteMap;
int getSite(int site){
if(siteMap.find(site) == siteMap.cend()){
siteMap[site] = siteCnt++;
}
return siteMap[site];
}
// 日期及其字段排序
struct Item{
int cnt;
int site;
Item(int site) : cnt(0), site(site){};
};
struct Date{
int date;
map<int, int> siteMap;
vector<Item> sites;
int cnt = 0;
void put(int site, int score){
if(this->siteMap.find(site) == this->siteMap.cend()){
this->siteMap[site] = cnt++;
sites.push_back(Item(site));
}
sites[this->siteMap[site]].cnt++;
}
}dates[MAX_N];
bool dateCmp(Item i1, Item i2){
return i1.cnt != i2.cnt ? i1.cnt > i2.cnt : i1.site < i2.site;
}
int dateCnt = 0;
map<int, int> dateMap;
int getDate(int date){
if(dateMap.find(date) == dateMap.cend()){
dateMap[date] = dateCnt++;
}
return dateMap[date];
}
map<char, vector<Record>> levels;
int main(){
levels['B'] = vector<Record>();
levels['A'] = vector<Record>();
levels['T'] = vector<Record>();
int N, M;
scanf("%d %d", &N, &M);
string card;
for(int i = 0, score, site, siteIndex, date; i < N; i++){
cin >> card >> score;
// 更新对应级别
levels[card[0]].push_back(Record(card, score));
// 更新对应考场
site = atoi(card.substr(1, 3).c_str());
siteIndex = getSite(site);
sites[siteIndex].cnt++;
sites[siteIndex].total += score;
// 更新对应日期
date = getDate(atoi(card.substr(4, 6).c_str()));
dates[date].put(site, score);
}
// 预先排序
sort(levels['B'].begin(), levels['B'].end(), recordCmp);
sort(levels['A'].begin(), levels['A'].end(), recordCmp);
sort(levels['T'].begin(), levels['T'].end(), recordCmp);
char L;
for(int i = 1, type, site, date; i <= M; i++){
scanf("%d ", &type);
switch (type) {
case 1:
L = getchar();
printf("Case %d: 1 %c\n", i, L);
if(levels[L].empty()){
printf("NA\n");
}else{
for(Record r: levels[L]){
printf("%s %d\n", r.card.c_str(), r.score);
}
}
break;
case 2:
scanf("%d", &site);
printf("Case %d: 2 %03d\n", i, site);
if(siteMap.find(site) == siteMap.cend()){
printf("NA\n");
}else{
Site& s = sites[siteMap[site]];
printf("%d %d\n", s.cnt, s.total);
}
break;
case 3:
scanf("%d", &date);
printf("Case %d: 3 %06d\n", i, date);
if(dateMap.find(date) == dateMap.cend()){
printf("NA\n");
}else{
vector<Item> &m = dates[dateMap[date]].sites;
sort(m.begin(), m.end(), dateCmp);// 对某一日期的考场信息进行排序
for(Item item : m){
printf("%03d %d\n", item.site, item.cnt);
}
}
default:
break;
}
}
return 0;
}