Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank
 

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4
 

题意:

有n个考场,每个考场有若干考生。

现给出各个考场所有考生的准考证号与分数,要求按分数从高到低分别进行①单考场内排序②总排序。

最终输出参加考试的考生数以及每个考生的①准考证号②总排名③考场号④考场内排名

 

思路:

1、排序的逻辑

①按分数从高到低排序

②分数相同时,按准考证号从小到大排序

2、所需要的信息及信息的存储

①对于单个学生student,需要对应的准考证号id,分数score,考场号location_number,考场内排名local_rank,总排名sum_rank——结构体Student,并创建一个足够大的结构体数组

②此外还需要总考场数n,总考生数num,单个考场内的考生数k——int型变量

3、排序逻辑的实现

 bool cmp(Student a, Student b) {
if (a.score != b.score) {
return a.score > b.score;
} else {
return strcmp(a.id, b.id) < ;
}
}

4、main()  分步分析

主要分为两个部分:

(1)给单个考场内的考生排名

①有n个考场,因此最外层要有for循环从1到n遍历每个考场

 for (int i = ; i <= n; i++) {
}

②每个考场内有k个学生,因此内层要有for循环从0到k-1遍历每个学生

 for (int j = ; j < k; j++) {
}

③接收输入的准考证号id与分数score,按顺序存入结构体数组中

 scanf("%s %d", stu[num].id, &stu[num].score);

④当前考场的学生遍历完后,调用排序函数sort()对当前考场内的所有考生按排序逻辑进行排序

 sort(stu, stu + k, cmp);

⑤按④的排序结果,依次给每个考生的考场内排名local_rank赋值

 stu[].local_rank = ;  //第一个考生的排名为1
for (int j = ; j < k; j++) {
if (stu[j].score == stu[j - ].score) {
stu[j].local_rank = stu[j - ].local_rank; //分数相同则排名相同
} else {
stu[j].local_rank = j + ; //分数不同则+1
}
}

(2)给所有的考生排名

⑥调用排序函数sort()对所有考生按排序逻辑进行排序(代码同④)

⑦按⑥的排序结果,依次给每个考生的总排名sum_rank赋值(代码同⑤)

5、踩到了第一个坑

只开一个Student类型的数组,但是要存放多个考场的考生信息,因此遍历和排序时不能简单的从下标0开始

解决:

引入新变量 总考生数num

于是可知,每次接收完一个新考场的考生数据时,此考场考生对应的下标范围是num - k ~ num

6、main()  雏形

 scanf("%d", &n); //考场数
for (int i = ; i <= n; i++) { //考场号,要从1开始
scanf("%d", &k); //当前考场人数 /*录入数据*/
for (int j = ; j < k; j++) {
scanf("%s %d", stu[num].id, &stu[num].score);
stu[num].location_number = i;
num++; //通过这个计算总考生数
} /*对刚录入的这个考场考生进行排序并赋名次*/
sort(stu + num - k, stu + num, cmp);
stu[num - k].local_rank = ;
for (int j = num - k + ; j < num; j++) {
if (stu[j].score == stu[j - ].score) {
stu[j].local_rank = stu[j - ].local_rank;
} else {
stu[j].local_rank = j + - (num - k);//j为此考生之前的人数,- (num - k)为去掉前面其他考场的人数,+ 1后即得到此考生在本考场的排名
}
} } /*对所有考生进行排序并赋名次*/
sort(stu, stu + num, cmp);
stu[].sum_rank = ;
for (int i = ; i < num; i++) {
if (stu[i].score == stu[i - ].score) {
stu[i].sum_rank = stu[i - ].sum_rank;
} else {
stu[i].sum_rank = i + ;
}
}

7、优化

总排名sum_rank是在整个程序的最后才得到的

它之后紧跟着的就是要将sum_rank输出——额外去存储的意义不大

并且sum_rank的值只有两种情况①和前一个相同②i + 1——值的计算简单,没有其他的相关项

所以考虑将“计算出总排名→存入sum_rank→输出stu[i].sum_rank”简化为“计算出总排名→输出总排名”

 sort(stu, stu + num, cmp);
int r = ; //r即为总排名,初始为1
for (int i = ; i < num; i++) {
//i > 0:从第二个考生开始(stu[0]为第一个考生)
//stu[i].score != stu[i - 1].score:
//若两人分数相同,则总排名r相同;
//若不同,则后一个人的排名为其前面的人数 i + 1;
if (i > && stu[i].score != stu[i - ].score) {
r = i + ;
}
}

8、main() 最终形态

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Student {
char id[];
int score;
int location_number;
int local_rank;
int sum_rank; //test
}stu[];
bool cmp(Student a, Student b) {
if (a.score != b.score) {
return a.score > b.score;
} else {
return strcmp(a.id, b.id) < ;
}
}
int main() {
int n, k, num = ;
scanf("%d", &n);
for (int i = ; i <= n; i++) {
scanf("%d", &k);
for (int j = ; j < k; j++) {
scanf("%s %d", stu[num].id, &stu[num].score);
stu[num].location_number = i;
num++;
}
sort(stu + num - k, stu + num, cmp);
stu[num - k].local_rank = ;
for (int j = num - k + ; j < num; j++) {
if (stu[j].score == stu[j - ].score) {
stu[j].local_rank = stu[j - ].local_rank;
} else {
stu[j].local_rank = j + - (num - k);
}
}
}
printf("%d\n", num);
sort(stu, stu + num, cmp);
int r = ;
for (int i = ; i < num; i++) {
if (i > && stu[i].score != stu[i - ].score) {
r = i + ;
}
printf("%s ", stu[i].id);
printf("%d %d %d\n", r, stu[i].location_number, stu[i].local_rank);
}
return ;
}

【算法学习记录-排序题】【PAT A1025】PAT Ranking的更多相关文章

  1. 【算法学习记录-排序题】【PAT A1012】The Best Rank

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

  2. 【算法学习记录-排序题】【PAT A1016】Phone Bills

    A long-distance telephone company charges its customers by the following rules: Making a long-distan ...

  3. 【算法学习记录-排序题】【PAT A1062】Talent and Virtue

    About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about ...

  4. 算法学习记录-排序——插入排序(Insertion Sort)

    插入排序: 在<算法导论>中是这样描述的 这是一个对少量元素进行排序的有效算法.插入排序的工作机理与打牌时候,整理手中的牌做法差不多. 在开始摸牌时,我们的左手是空的,牌面朝下放在桌子上. ...

  5. 算法学习记录-排序——冒泡排序(Bubble Sort)

    冒泡排序应该是最常用的排序方法,我接触的第一个排序算法就是冒泡,老师也经常那这个做例子. 冒泡排序是一种交换排序, 基本思想: 通过两两比较相邻的记录,若反序则交换,知道没有反序的记录为止. 例子: ...

  6. 算法学习记录-排序——选择排序(Simple Selection Sort)

    之前在冒泡排序的附录中提到可以在每次循环时候,不用交换操作,而只需要记录最小值下标,每次循环后交换哨兵与最小值下标的书, 这样可以减少交换操作的时间. 这种方法针对冒泡排序中需要频繁交换数组数字而改进 ...

  7. 算法学习记录-图——应用之拓扑排序(Topological Sort)

    这一篇写有向无环图及其它的应用: 清楚概念: 有向无环图(DAG):一个无环的有向图.通俗的讲就是从一个点沿着有向边出发,无论怎么遍历都不会回到出发点上. 有向无环图是描述一项工程或者系统的进行过程的 ...

  8. 算法学习 拓扑排序(TopSort)

    拓扑排序 一.基本概念 在一个有向无环图(Directed Acyclic Graph, DAG)中,规定< u,v > 表示一条由u指向v的的有向边.要求对所有的节点排序,使得每一条有向 ...

  9. Manacher回文串算法学习记录

    FROM:  http://hi.baidu.com/chenwenwen0210/item/482c84396476f0e02f8ec230 #include<stdio.h> #inc ...

随机推荐

  1. 使用placeholder属性设置input文本框的提示信息

    input文本框中设置提示信息,可以使用placeholder属性 <!DOCTYPE html> <html> <head> <meta charset=& ...

  2. cf1280B

    题意:给出一个n*m的矩阵,矩阵中的元素要么P要么是A 每次可以选择一个的子矩形,然后将矩阵中每一行都变为第一行,或者将矩阵中每一列都变为第一列 要求用最少的次数将矩阵中所有元素都变成A 题解:分类讨 ...

  3. CSS操作

    CSS 与 JavaScript 是两个有着明确分工的领域,前者负责页面的视觉效果,后者负责与用户的行为互动.但是,它们毕竟同属网页开发的前端,因此不可避免有着交叉和互相配合. 1. 使用JavaSc ...

  4. 一键安装最新内核并开启 BBR 脚本

    最近,Google 开源了其 TCP BBR 拥塞控制算法,并提交到了 Linux 内核,从 4.9 开始,Linux 内核已经用上了该算法.根据以往的传统,Google 总是先在自家的生产环境上线运 ...

  5. kali安装mongodb

    kali安装mongodb 1. 从官网下载需要的安装包 官网下载地址:https://www.mongodb.com/download-center/community 下载完后可以直接用xshel ...

  6. CF895C Square Subsets [线性基]

    线性基的题- 考虑平方数只和拆解质因子的个数的奇偶性有关系 比如说你 \(4\) 和 \(16\) 的贡献都是一样的.因为 \(4 = 2^2 , 16 = 2^4\) \(2\) 和 \(4\) 奇 ...

  7. Docker常用命令和功能介绍

    可以搜索 dockerfile 定制创建一个redis镜像image 表示镜像docker search 搜索镜像的名称和标签docker 所在目录/var/lib/dockerdocker的镜像文件 ...

  8. [POI2010] GIL-Guilds - 二分图染色,DFS

    给一张无向图,要求你用黑白灰给点染色,且满足对于任意一个黑点,至少有一个白点和他相邻:对于任意一个白点,至少有一个黑点与他相邻,对于任意一个灰点,至少同时有一个黑点和白点和灰点与他相邻,问能否成功 S ...

  9. php 字符串常用函数

    数组.字符串和数据库是我们函数里面最.最.最常用的三类函数. 当然PHP的字符串函数也有很多.我们最常使用的两个系列的字符串: 1.单字节字符串处理函数 2.多字节字符串处理函数 3.字符串编码转换函 ...

  10. 深入浅出Mybatis系列七-mapper映射文件配置之insert、update、delete

    注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 上篇文章<深入浅出Mybatis系列(六)---objectFactory.p ...