【解析】1012 The Best Rank (25 分)
立志用最少的代碼做最高效的表達
PAT甲級最優題解——>傳送門
To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks – that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.
For example, The grades of C, M, E and A - Average of 4 students are given as the following:
StudentID C M E A
310101 98 85 88 90
310102 70 95 88 84
310103 82 87 94 88
310104 91 91 91 91
Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.
Output Specification:
For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.
The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.
If a student is not on the grading list, simply output N/A.
Sample Input:
5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999
Sample Output:
1 C
1 M
1 E
1 A
3 A
N/A
注意:如果兩個分數相同,他們都是同一名,例如:100,100,90,為1、1、3。
#include<bits/stdc++.h> using namespace std; string intTochar="ACME";//將數組下標映射到相應字符 typedef pair<int,double> StudentScore;//存儲學生id和分數的結構體 unordered_map<int,array<int,4>>studentRank;//存儲學生id和4門成績的排名,4門成績排名存儲在一個數組中,下標0123分別代表ACME四個成績排名 int N,MM,id;//學生總數和要查詢的學生數量 void setRank(const vector<StudentScore>&temp,int index){//找出學生在某一成績上的排名,index值為0123,分別代表ACME四個成績排名studentRank[temp[0].first][index]=1;for(int i=1;i<temp.size();++i){if(temp[i].second!=temp[i-1].second)studentRank[temp[i].first][index]=i+1;elsestudentRank[temp[i].first][index]=studentRank[temp[i-1].first][index];} } int main(){scanf("%d%d",&N,&MM);vector<StudentScore>ACME[4];//下標為0123的vector分別存儲ACME四個成績for(int i=0;i<N;++i){double A[4];//下標為0123的元素分別臨時存儲ACME四個成績scanf("%d%lf%lf%lf",&id,&A[1],&A[2],&A[3]);A[0]=(A[1]+A[2]+A[3])/3;for(int i=0;i<4;++i)ACME[i].push_back({id,A[i]});}for(int i=0;i<4;++i)//進行排序sort(ACME[i].begin(),ACME[i].end(),[](const StudentScore&s1,const StudentScore&s2){//比較函數,按分數由大到小排序,分數相同按id從小到大排序return s1.second!=s2.second?s1.second>s2.second:s1.first<s2.first;});for(int i=0;i<4;++i)setRank(ACME[i],i);while(MM--){scanf("%d",&id);if(studentRank.find(id)==studentRank.cend()){//如果沒有這樣的學生,輸出N/Aprintf("N/A\n");}else{//如果有這樣的學生auto i=min_element(studentRank[id].begin(),studentRank[id].end());//標準庫函數,按ACME優先級順序找到排名最靠前的迭代器printf("%d %c\n",*i,intTochar[i-studentRank[id].begin()]);//輸出結果}}return 0; }
總結
以上是生活随笔為你收集整理的【解析】1012 The Best Rank (25 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1011 World Cup Betti
- 下一篇: 【解析】1013 Battle Over