A registration card number of PAT consists of 4 parts:
T
for the top level, A
for advance and B
for basic;yymmdd
;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.
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 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.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:
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);Nt Ns
where Nt
is the total number of testees and Ns
is their total score;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
.
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
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
1.准考证带字母
只能用string存准考证,所以还得弄个结构体,存准考证和分数
2.要求排序
就因为这个排序,一定要用结构体和vector,在自定义一个排序函数
3.收集、计算
如果是收集(查询1),那只用结构体和vector没有问题。
如果是计算(查询3),因为结构体和vector的成员值是只读的(我试过了,成员值只能局部改,出了框改不了),只能用map。而map会超时(很惨了),所以只能用unodered_map了(头文件同名,别忘了加)。
1.substr
字符串截断,PAT常见操作
2.for(auto i:v)
如果用不到STL容器的编号就可以用这个c++14的枚举特性
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
int n,m,k,num=0,total=0;
string s;
struct node{
string id;
int value;
};
bool cmp(node &a,node &b){
return a.value==b.value?a.id<b.id:a.value>b.value;
}
int main(){
scanf("%d %d",&n,&m);
vector<node> v(n);
for(int i=0;i<n;i++){
cin>>v[i].id>>v[i].value;
}
for(int i=1;i<=m;i++){
cin>>k>>s;
printf("Case %d: %d %s\n",i,k,s.c_str());
vector<node> ans;
if(k==1){
for(auto it:v){
if(it.id.substr(0,1)==s){
ans.push_back(it);
}
}
if(ans.size()==0){
printf("NA\n");
continue;
}
sort(ans.begin(),ans.end(),cmp);
for(auto j:ans){
printf("%s %d\n",j.id.c_str(),j.value);
}
}else if(k==2){
num=0;
total=0;
for(auto it:v){
if(it.id.substr(1,3)==s){
num++;
total+=it.value;
}
}
if(num==0){
printf("NA\n");
continue;
}
printf("%d %d\n",num,total);
}else if(k==3){
unordered_map<string, int> ans3;
for(auto it:v){
if(it.id.substr(4,6)==s){
ans3[it.id.substr(1,3)]++;
}
}
if(ans3.size()==0){
printf("NA\n");
continue;
}
for(auto it:ans3){
ans.push_back({it.first,it.second});
}
sort(ans.begin(),ans.end(),cmp);
for(auto j:ans){
printf("%s %d\n",j.id.c_str(),j.value);
}
}
}
return 0;
}
如果喜欢,请给我点赞或者评论,感谢