题目
大意:有N个学生,M次询问。
N个学生,每人有一个6位数的学号,3个科目的成绩,分别有相应的优先级。3个成绩还有一个平均成绩
每一个学生有4个排名,平均成绩排名,和3个科目的单独排名。
在M次询问中,每次问一个学生的学号,这个学号的学生最好的排名是多少,对应的科目是什么或者是不是平均成绩?
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”.
分析
这么长时间没有练习,这个代码写的又臭又长。还大材小用,使用了优先队列。
由于不知道优先队列怎么按照4个成绩进行排序,于是定义了4个结构体,4个有限队列 , 4个数组存储(下标是学号,元素时排名)
之间有1 , 2测试点没有通过。
百度了一下,发现自己没有考虑相同成绩的情况。相同的成绩下,排名也应该相同。要注意的是,排名应该是:1 1 3 4 5,而不是1 ,1, 2 , 3, 4。就是注意相同排名的下一个位次到底是多少。
别人简化的代码。这里flag的数值对应score的下标
bool cmp1(node a, node b) {return a.score[flag] > b.score[flag];}
for(int i = 0; i < n; i++)
{
scanf("%d %d %d %d", &stu[i].id, &stu[i].score[1],
&stu[i].score[2], &stu[i].score[3]);
stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3])
/ 3.0 + 0.5;
}
for(flag = 0; flag <= 3; flag++)
{
sort(stu, stu + n, cmp1);
stu[0].rank[flag] = 1;
for(int i = 1; i < n; i++) {
stu[i].rank[flag] = i + 1;
if(stu[i].score[flag] == stu[i-1].score[flag])
stu[i].rank[flag] = stu[i-1].rank[flag];
}
}
需不需要四舍五入的,像我写成double也没问题。
代码
特别不好意思放上自己的代码
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
using namespace std;
const int maxn = 1e6 + 5;
int N , M;
struct StudentA
{
int id;
int C , M ,E ;
double A;
friend bool operator < (StudentA a , StudentA b)
{
return a.A < b.A;
}
};
priority_queue<StudentA> qA;
struct StudentC
{
int id;
int C , M ,E ;
double A;
friend bool operator < (StudentC a , StudentC b)
{
return a.C < b.C;
}
};
priority_queue<StudentC> qC;
struct StudentM
{
int id;
int C , M ,E ;
double A;
friend bool operator < (StudentM a , StudentM b)
{
return a.M < b.M;
}
};
priority_queue<StudentM> qM;
struct StudentE
{
int id;
int C , M ,E ;
double A;
friend bool operator < (StudentE a , StudentE b)
{
return a.E < b.E;
}
};
priority_queue<StudentE> qE;
int rankA[maxn];
int rankC[maxn];
int rankM[maxn];
int rankE[maxn];
int main()
{
cin >> N >> M;
for(int i = 0 ; i < N ; i ++)
{
int id , c , m , e ;
cin >> id >> c >> m >>e;
double a = ( c + m + e) / 3.0;
StudentA tmpA = {id , c , m , e ,a};
StudentC tmpC = {id , c , m , e ,a};
StudentM tmpM = {id , c , m , e ,a};
StudentE tmpE = {id , c , m , e ,a};
qA.push(tmpA);
qC.push(tmpC);
qM.push(tmpM);
qE.push(tmpE);
}
int idx = 1;
int lastscore = 0;
int first = 1;
int idx2 = 0;
while(!qA.empty())
{
StudentA tmp = qA.top();
qA.pop();
idx2 ++;
if(first)
{
lastscore = tmp.A;
first = 0;
rankA[tmp.id] = 1;
continue;
}
if(lastscore == tmp.A)
{
rankA[tmp.id] = idx;
lastscore = tmp.A;
}
else
{
rankA[tmp.id] = idx2;
idx = idx2;
lastscore = tmp.A;
}
}
// for(int i = 1 ; i <= N ; i++)
// cout << rankA[i ] << " ";
idx = 1;
lastscore = 0;
first = 1;
idx2 = 0;
while(!qC.empty())
{
StudentC tmp = qC.top();
qC.pop();
idx2 ++;
if(first)
{
lastscore = tmp.C;
first = 0;
rankC[tmp.id] = 1;
continue;
}
if(lastscore == tmp.C)
{
rankC[tmp.id] = idx;
lastscore = tmp.C;
}
else
{
rankC[tmp.id] = idx2;
idx = idx2;
lastscore = tmp.C;
}
}
idx = 1;
lastscore = 0;
first = 1;
idx2 = 0;
while(!qM.empty())
{
StudentM tmp = qM.top();
qM.pop();
idx2 ++;
if(first)
{
lastscore = tmp.M;
first = 0;
rankM[tmp.id] = 1;
continue;
}
if(lastscore == tmp.M)
{
rankM[tmp.id] = idx;
lastscore = tmp.M;
}
else
{
rankM[tmp.id] = idx2;
idx = idx2;
lastscore = tmp.M;
}
}
idx = 1;
lastscore = 0;
first = 1;
idx2 = 0;
while(!qE.empty())
{
StudentE tmp = qE.top();
qE.pop();
idx2 ++;
if(first)
{
lastscore = tmp.E;
first = 0;
rankE[tmp.id] = 1;
continue;
}
if(lastscore == tmp.E)
{
rankE[tmp.id] = idx;
lastscore = tmp.C;
}
else
{
rankE[tmp.id] = idx2;
idx = idx2;
lastscore = tmp.E;
}
}
for(int i = 0 ; i < M; i++)
{
int need;
cin >> need;
int ans = min( min(rankA[need] , rankC[need]), min(rankM[need] , rankE[need]));
// cout << rankA[need] << ' ' << rankC[need] <<' ' << rankM[need] << ' ' << rankE[need] << endl;
if(ans == 0)
{
cout << "N/A" << endl;
continue;
}
else
{
if( ans == rankA[need])
cout << rankA[need] << " " << 'A' << endl;
else if( ans == rankC[need])
cout << rankC[need] << " " << 'C' << endl;
else if( ans == rankM[need])
cout << rankM[need] << " " << 'M' << endl;
else if( ans == rankE[need])
cout << rankE[need] << " " << 'E' << endl;
}
}
}