PAT A1012 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 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
题意
每名学生有 C
M
E
三门成绩以及平均成绩 A
求在所有学生中某一项排名最靠前的
思路1
- 将每名学生存储成一个
map
映射,为id
对应其四门成绩vector<int>
类型的 vector<int> grades[4]
则存储四门课程所有的学生的单门课程成绩并排序- 对于每一个查询,先看
map
中是否存在该名学生,如存在分别计算出每门课程的成绩,取排名最高的一个
注意点:
- 四舍五入使用
round
浮点数 - 临时的
temp
处理输入需要进行初始化0
- 默认升序,排名 =
i+1
代码1
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cmath>
using namespace std;
unordered_map<string, vector<int>> mp; // 学生--成绩
vector<int> grades[4]; // 四门课程所有成绩
char names[] = "ACME";
int get_rank(const vector<int> &v, int x)
{
for(int i = 0; i < v.size(); i++)
{
if(v[i] == x)
return i + 1;
}
return -1;
}
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
int n, m;
cin >> n >> m;
// 输入
for(int i = 0; i < n; i++)
{
string id;
int temp[4] = {0}; // ~~初始化
cin >> id;
for(int i = 1; i <= 3; i++)
{
cin >> temp[i];
temp[0] += temp[i];
}
temp[0] = round(temp[0]/3.0); // 四舍五入取成绩
for(int i = 0; i < 4; i++)
{
mp[id].push_back(temp[i]); // 学生的四门成绩映射
grades[i].push_back(temp[i]); // 每一门课程
}
}
// 排序
for(int i = 0; i < 4; i++)
sort(grades[i].begin(), grades[i].end(), cmp);
// 查询
while(m--)
{
string id;
cin >> id;
if(mp.find(id) == mp.end())
{
cout << "N/A" << endl;
}
else
{
// 找最高排名
int ans = n + 1; // max
char c;
for(int i = 0; i < 4; i++) // 四门课程
{
int r = get_rank(grades[i], mp[id][i]);
if(r < ans)
{
ans = r;
c = names[i];
}
}
cout << ans << " " << c << endl;
}
}
return 0;
}
思路2
整体思路大致一样,但是由于 grades[i]
是有序的,因此可以使用 二分
来查找
代码2
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cmath>
using namespace std;
unordered_map<string, vector<int>> mp; // 学生--成绩
vector<int> grades[4]; // 四门课程所有成绩
char names[] = "ACME";
int get_rank(const vector<int> &v, int x)
{
int l = 0, r = v.size() - 1;
while(l < r)
{
int mid = (l + r + 1) >> 1;
if(v[mid] <= x) l = mid;
else r = mid - 1;
}
return v.size() - l;
}
int main()
{
int n, m;
cin >> n >> m;
// 输入
for(int i = 0; i < n; i++)
{
string id;
int temp[4] = {0}; // ~~初始化
cin >> id;
for(int i = 1; i <= 3; i++)
{
cin >> temp[i];
temp[0] += temp[i];
}
temp[0] = round(temp[0]/3.0); // 四舍五入取成绩
for(int i = 0; i < 4; i++)
{
mp[id].push_back(temp[i]); // 学生的四门成绩映射
grades[i].push_back(temp[i]); // 每一门课程
}
}
// 排序
for(int i = 0; i < 4; i++)
sort(grades[i].begin(), grades[i].end());
// 查询
while(m--)
{
string id;
cin >> id;
if(mp.find(id) == mp.end())
{
cout << "N/A" << endl;
}
else
{
// 找最高排名
int ans = n + 1; // max
char c;
for(int i = 0; i < 4; i++) // 四门课程
{
int r = get_rank(grades[i], mp[id][i]);
if(r < ans)
{
ans = r;
c = names[i];
}
}
cout << ans << " " << c << endl;
}
}
return 0;
}
去掉 t[0] = round(t[0] / 3.0);为啥就错了?
平均分排名 跟 总分排名 不一个含义吗?为啥改用总分排名就会错?
因为删了就少了四舍五入的操作了
老哥,二分那里的判断还是很迷呀。我改成这样 含义也对啊。但是错了,。。。
这个传入的vector是升序排列,所以找的是>= 当前x的最小值,也就是传入的vector的最右边的数。如果二分按照你这样找的话,找的是数组最左边的数,这样的话排名相同会出现错误。
请问一下,二分的那个判断条件是怎么想的啊…
就是根据y总在基础课中讲的二分的思想,找一个
check
函数,二分出来边界点。因为默认的是升序排列,因此我们要找到同一个分数最右边的数才能表示排名,最后找到的l
或者r
表示的是下标,返回排名v.size() -
(从1开始的)