立志用最少的代码做最高效的表达


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 分)相关推荐

  1. PAT 1012 The Best Rank (25 分)

    1012 The Best Rank (25 分) 今天给大家分享的是PAT甲级的一道小题,设计题 原题请点击我 简单翻译: 设计一个排名表,这个表中以学生ID为主键,C表示程序设计语言的成绩,M表示 ...

  2. 1012 The Best Rank (25 分)【难度: 中 / 知识点: 排序 前缀和】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805502658068480 解析: 这里的每一项排名有一个坑的点,就是 ...

  3. 【PAT甲级 排序】1012 The Best Rank (25 分) C++ 全部AC

    题目 中规中矩的一道题.排名的顺序一开始没太明白,但是理解之后写起来挺流畅的,没什么坑. 解题思路:题目会给出所有学生所有科目的成绩.想要看分学生的学号. 我们要先给这些学生单科成绩排序,算出它们的单 ...

  4. PAT甲级真题(结构体排序)——1012. The Best Rank (25)

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

  5. 【PAT - 甲级1012】The Best Rank (25分)

    题干: To evaluate the performance of our first year CS majored students, we consider their grades of t ...

  6. 【最详细解析】1070 结绳 (25分)_18行代码AC

    立志用更少的代码做更高效的表达 Pat乙级最优化代码+题解+分析汇总-->传送门 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的 ...

  7. 【最全解析】1050 螺旋矩阵 (25分)

    立志用更少的代码做更高效的表达 Pat乙级最优化代码+题解+分析汇总-->传送门 本题要求将给定的 N 个正整数按非递增的顺序,填入"螺旋矩阵".所谓"螺旋矩阵&q ...

  8. 1012 The Best Rank (25)

    题目描述: To evaluate the performance of our first year CS majored students, we consider their grades of ...

  9. L2-031 深入虎穴 (25 分)(DFS 代码有详细解析)

    L2-031 深入虎穴 (25 分) 著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报.已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门.每一扇门背后或者是 ...

最新文章

  1. 吴恩达:AI未来将呈现四大发展趋势
  2. win8系统ghost后只有一个盘了其它分区的文件如何找回
  3. 2021年人工神经网络第四次作业:基于MATLAB的求解
  4. python使用input函数时、必须添加提示文字-python input函数
  5. Julia程序设计1 介绍和基础数学运算
  6. android恶意代码检测报告,用机器学习检测Android恶意代码
  7. 小米、360、京东之后 阿里联手美的杀入智能家居市场
  8. 每日一道算法题-寻找丑数
  9. tmux 如何自定义背景颜色 | How does the tmux color palette work?
  10. python声音信号调制_用python产生正弦波和PWM信号产生脉冲幅度调制
  11. java 超时异常_Java如何实现任务超时处理
  12. 最新可用的goole翻译接口
  13. pdf 深入理解kotlin协程_协程初探
  14. 普通摄像头的数据输出格式YUV与mjpeg之间联系、DCT离散余弦变换去噪跟压缩(待补充)
  15. Guava库学习:学习Concurrency(二)Monitor_2
  16. push msg php_php进程通信-消息队列
  17. 鼠标事件mousemove、mouseover、mouseout、mouseenter、mouseleave
  18. OLED调试和OLED显示器(程序)(学习笔记)
  19. jQuery的下载与安装
  20. 01 基础入门:概念名词

热门文章

  1. Ubuntu LXC
  2. ProtoBuf的使用以及原理分析
  3. 设计模式:备忘录模式(Memento)
  4. OS--进程间通信详解(一)
  5. Facebook如何将QUIC应用于数十亿流量传输
  6. 我在攻读计算机视觉和机器学习硕士学位时学到了什么
  7. 从网络、编码、内容感知、存储、分发看视频云端到端技术实践
  8. 你有一个向LiveVideoStackCon讲师提问的机会
  9. MPEG创始人、主席:MEPG商业模式已经破裂
  10. 火速围观!鹅厂中间件产品遭遇暴风吐槽