1012 The Best Rank (25)
Posted mr-stn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1012 The Best Rank (25)相关的知识,希望对你有一定的参考价值。
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 Algebra), 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
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
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
题目大意:给出每个学生的c语言,数学,英语成绩。需要计算这三门学科的平均分。给出某个学生的id,找出平均分,c,数学,英语中名次最高的学科以及名次,四者的优先级依次降低;
思路:建立一个node类型来保存每一个学生的成绩grade[4](依次记录平均分,c,数学, 英语), 名次rank[4]. 对学生的成绩进行四次排序,并把对应的名次记录在rank[4]中。
四次排序完成后,再遍历所有人,记录每一个人在数组中的位置,以及找出每一个学生最高名次对应的学科,记录在best中
小窍门:在确定学生的最好名次的时候,顺便把学生在数组中的位置记录在另外一个数组中,exist[i]=j;表示id为i的人在数组中的位置为j。这样再后面的query中能迅速找到学生在数组中所在位置。
参考地址:https://www.liuchuo.net/archives/2207;
1 #include<iostream> 2 #include<iostream> 3 #include<algorithm> 4 #include<vector> 5 using namespace std; 6 struct node{ 7 int id, best; 8 int grade[4], rank[4]; 9 }; 10 11 int index; 12 bool cmp(node a, node b){return a.grade[index]>b.grade[index];} 13 14 int main(){ 15 int n, m, i; 16 cin>>n>>m; 17 vector<node> v(n); 18 vector<int> exist(1000000, -1); 19 char ch[4]={‘A‘,‘C‘,‘M‘,‘E‘}; 20 for(i=0; i<n; i++) { 21 scanf("%d %d %d %d", &v[i].id, &v[i].grade[1], &v[i].grade[2], &v[i].grade[3]); 22 v[i].grade[0]=(v[i].grade[3]+v[i].grade[1]+v[i].grade[2])/3; 23 } 24 for(index=0; index<4; index++){ 25 sort(v.begin(), v.end(), cmp); 26 v[0].rank[index]=1; 27 for(i=1; i<n; i++){ 28 if(v[i].grade[index]<v[i-1].grade[index]) v[i].rank[index]=i+1; 29 else v[i].rank[index]=v[i-1].rank[index]; 30 } 31 } 32 for(i=0; i<n; i++){ 33 int minn=0; 34 exist[v[i].id]=i; 35 for(int j=0; j<4; j++) if(v[i].rank[j]<v[i].rank[minn]) minn=j; 36 v[i].best=minn; 37 } 38 int temp; 39 for(i=0; i<m; i++){ 40 scanf("%d", &temp); 41 int x=exist[temp]; 42 if(x!=-1) cout<<v[x].rank[v[x].best]<<" "<<ch[v[x].best]<<endl; 43 else cout<<"N/A"<<endl; 44 } 45 return 0; 46 }
感觉排序类的问题不是很难,但是细节很多,每次都不能完全考虑,花费的时间比较多,以后多做这方面的思维练习
以上是关于1012 The Best Rank (25)的主要内容,如果未能解决你的问题,请参考以下文章