题目描述
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.
输入
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.
输出
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.
思路
其实就是排序题,题目难点在于要输出序号,序号呢同分的序号一样,就比如A考了99,B考了99,C考了98,则A,B的排名为1,C的排名为3。其实这种输出序号也不难,可以用一个值标记前一个的分数,若与前一个分数一样则排名不变,否则排名等于当前的索引。
代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stdlib.h>
#include<vector>
#include<string.h>
using namespace std;
typedef struct node {
char id[20];
int final_rank;
int group;
int group_rank;
int score;
}PAT;
vector<PAT> pat;
bool cmp1(PAT a, PAT b)
{
return a.score > b.score;
}
bool cmp2(const PAT& a, const PAT& b)
{
if (a.score != b.score)
{
return a.score > b.score;
}
else
{
if (strcmp(a.id, b.id) < 0)
{
return true;
}
else
{
return false;
}
}
}
int main()
{
int N;
cin >> N;
for (int i = 0; i < N; i++)
{
int M;
cin >> M;
PAT p[1000];
for (int j = 0; j < M; j++)
{
getchar();
cin >> p[j].id >> p[j].score;
p[j].group = i + 1;
}
sort(p, p + M,cmp1);
p[0].group_rank = 1;
for (int j = 1; j < M; j++)
{
if (p[j].score == p[j - 1].score)
{
p[j].group_rank = p[j - 1].group_rank;
}
else
{
p[j].group_rank = j + 1;
}
}
for (int j = 0; j < M; j++)
{
pat.push_back(p[j]);
}
}
sort(pat.begin(), pat.end(), cmp2);
pat[0].final_rank = 1;
for (int i = 1; i < pat.size(); i++)
{
if (pat[i].score == pat[i - 1].score)
{
pat[i].final_rank = pat[i - 1].final_rank;
}
else
{
pat[i].final_rank = i + 1;
}
}
printf("%d\n", pat.size());
for (int i = 0; i < pat.size(); i++)
{
printf("%s %d %d %d\n", pat[i].id, pat[i].final_rank, pat[i].group, pat[i].group_rank);
}
}