当前位置: 代码迷 >> 综合 >> 1025 PAT Ranking(25 分)
  详细解决方案

1025 PAT Ranking(25 分)

热度:92   发布时间:2023-12-25 03:29:43.0

方法一:
思路分析:编写归并排序函数,用递归方式实现;
注意:id用long long型读入时,输出格式要控制位%013lld
方法二:
用STL库的sort函数
方法1代码:

#include<stdio.h>
const int maxn = 30010;
struct node {int score, f_r, loc, l_r;long long id;
}info[maxn];
void merge(int L1, int R1, int L2, int R2) {int i = L1, j = L2, index = 0;node temp_info[maxn];while (i <= R1 && j <= R2) {if ((info[i].score > info[j].score)|| (info[i].score == info[j].score && info[i].id<info[j].id)) {temp_info[index++] = info[i++];}else {temp_info[index++] = info[j++];}}while (i <= R1) {temp_info[index++] = info[i++];}while (j <= R2) {temp_info[index++] = info[j++];}for (int i = 0; i < index; i++) {info[L1 + i] = temp_info[i];}
}
void mergeSort(int left, int right) {if (left < right) {int mid = (left + right) / 2;mergeSort(left, mid);mergeSort(mid + 1, right);merge(left, mid, mid + 1, right);}
}
int main()
{int n, k, cnt = 0;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &k);for (int j = 0; j < k; j++) {scanf("%lld %d", &info[cnt].id, &info[cnt].score);info[cnt].loc = i + 1;cnt++;}mergeSort(cnt - k, cnt-1);info[cnt - k].l_r = 1;for (int j = 1; j < k; j++) {if (info[cnt - k + j].score != info[cnt - k + j - 1].score) {info[cnt - k + j].l_r = j + 1;}else {info[cnt - k + j].l_r = info[cnt - k + j - 1].l_r;}}}mergeSort(0 , cnt - 1);info[0].f_r = 1;for (int i = 1; i < cnt; i++) {if (info[i].score != info[i - 1].score) {info[i].f_r = i + 1;}else {info[i].f_r = info[i - 1].f_r;}}printf("%d\n", cnt);for (int i = 0; i < cnt; i++) {printf("%013lld %d %d %d\n", info[i].id, info[i].f_r, info[i].loc, info[i].l_r);}return 0;
}

方法2代码:


#include"stdio.h"
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn = 30010;
struct node {int score, f_r, loc, l_r;long long id;
}info[maxn];
bool cmp(node a, node b) {if (a.score != b.score) {return a.score > b.score;}else {return a.id < b.id;}
}
int main()
{int n, k, cnt = 0;scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d", &k);for (int j = 0; j < k; j++) {scanf("%lld %d", &info[cnt].id, &info[cnt].score);info[cnt].loc = i;cnt++;}sort(info + cnt - k, info + cnt, cmp);info[cnt - k].l_r = 1;for (int j = cnt - k + 1; j < cnt; j++) {if (info[j].score == info[j - 1].score) {info[j].l_r = info[j - 1].l_r;}else {info[j].l_r = j + 1 - (cnt - k);}}}sort(info, info + cnt, cmp);info[0].f_r = 1;for (int i = 1; i < cnt; i++) {if (info[i].score == info[i - 1].score) {info[i].f_r = info[i - 1].f_r;}else {info[i].f_r = i + 1;}}printf("%d\n", cnt);for (int i = 0; i < cnt; i++) {printf("%013lld %d %d %d\n", info[i].id, info[i].f_r, info[i].loc, info[i].l_r);}return 0;
}

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