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 CME 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 CM 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

#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = ;
struct stu{
int id;
int grade[];
};
vector<stu> vs;
int myrank[maxn][];
char list[] = { 'A', 'C', 'M', 'E' };
int now;
bool cmp(stu s1, stu s2){
return s1.grade[now] > s2.grade[now];
}
int main(){
int n, m;
scanf("%d %d", &n, &m);
for (int i = ; i<n; i++){
int id;
int c, m, e, a;
scanf("%d", &id);
scanf("%d %d %d\n", &c, &m, &e);
stu s;
s.id = id;
s.grade[] = c;
s.grade[] = m;
s.grade[] = e;
a = c + m + e;
s.grade[] = a;
vs.push_back(s);
}
for (now = ; now < ; now++){
sort(vs.begin(), vs.end(), cmp);
myrank[vs[].id][now] = ;
for (int k = ; k < n; k++){
if (vs[k].grade[now] != vs[k - ].grade[now]){
myrank[vs[k].id][now] = k + ;
}
else{
myrank[vs[k].id][now] = myrank[vs[k-].id][now];
}
}
}
for (int i = ; i < m; i++){
int j;
scanf("%d", &j); if (myrank[j][] != ){
int best = maxn, best_k = ;;
for (int k = ; k < ; k++){
if (myrank[j][k]<best){
best_k = k;
best = myrank[j][k];
}
}
printf("%d %c\n", myrank[j][best_k], list[best_k]);
}
else{
printf("N/A\n");
}
}
}

注意点:很简单的一道题目,却花了一个多小时,一开始题目意思理解错了,虽然就算理解对了,也一样。就觉得很麻烦,要开好多个数组,加好多属性,总觉得可能有捷径,就一直不下手。反思一下,就是要用最笨的最麻烦的方法先写出来,如果超时了,再去想哪里可以改进。这道题就是直接开个记录排名的数组,或者在结构体里加上各个成绩的排名属性,每个都排一下序,记录下来。这里唯一的小技巧是记录成绩用一个数组,而不是4个int,这样写cmp函数只要写一个。

PAT A1012 The Best Rank (25 分)——多次排序,排名的更多相关文章

  1. PAT-1012 The Best Rank (25 分) 查询分数对应排名(包括并列)

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  2. PAT A1146 Topological Order (25 分)——拓扑排序,入度

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  3. A1012 The Best Rank (25)(25 分)

    A1012 The Best Rank (25)(25 分) To evaluate the performance of our first year CS majored students, we ...

  4. 1012 The Best Rank (25分) vector与结构体排序

    1012 The Best Rank (25分)   To evaluate the performance of our first year CS majored students, we con ...

  5. PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值

    题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...

  6. PAT A1122 Hamiltonian Cycle (25 分)——图遍历

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  7. PAT A1142 Maximal Clique (25 分)——图

    A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the ...

  8. [PAT] 1142 Maximal Clique(25 分)

    1142 Maximal Clique(25 分) A clique is a subset of vertices of an undirected graph such that every tw ...

  9. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  10. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

随机推荐

  1. 【github&&git】4、git常用命令(持续更新中)

    git常用命令(持续更新中) 本地仓库操作git int                                 初始化本地仓库git add .                       ...

  2. Java-IO:复制文件

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...

  3. js .map方法

    map这里的map不是“地图”的意思,而是指“映射”.[].map(); 基本用法跟forEach方法类似: array.map(callback,[ thisObject]); callback的参 ...

  4. Python 单例设计模式

    class Foo: def __init__(self, name, age): self.name = name self.age = age def show(self): print(self ...

  5. Vue Router滚动行为 scrollBehavior

    滚动行为 使用前端路由,当切换到新路由时,想要页面滚动到顶部或者是保持原先的滚动位置,就像重新加载页面那样. vue-router能做到,而且更好,它让你可以自定义路由切换时页面如何滚动. 注意:这个 ...

  6. Kotlin入门(20)几种常见的对话框

    提醒对话框手机上的App极大地方便了人们的生活,很多业务只需用户拇指一点即可轻松办理,然而这也带来了一定的风险,因为有时候用户并非真的想这么做,只是不小心点了一下而已,如果App不做任何提示的话,继续 ...

  7. Spring boot 入门篇

    详见:https://www.cnblogs.com/ityouknow/p/5662753.html 什么是Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架, ...

  8. C#基础(数据类型运算符)

    ---恢复内容开始--- 1.类 修饰符 class 类名 基类或接口 { } 2.命名规范 成员变量前加_ 首字符小写,后面单词首字母大写(Camel规则) 接口首字母为I 方法的命名使用动词 所有 ...

  9. 将爬取的数据保存到mysql中

    为了把数据保存到mysql费了很多周折,早上再来折腾,终于折腾好了 安装数据库 1.pip install pymysql(根据版本来装) 2.创建数据 打开终端 键入mysql -u root -p ...

  10. git 创建本地仓库、远程仓库,上传项目

    1.在本地想创建git仓库的地方创建本地仓库 首先右键打开 Git Bash Here,如果没有,请先安装git,下载地址:https://git-scm.com/downloads git init ...