题目
1025 PAT Ranking (25分)
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,每个考场有K个人,然后输出每个人的考试号和成绩;要求输出考试人数总和,按行输出每个学生的 考试号、成绩总排名、考场号、自己考场内排名;
其中相同成绩的学生排名相同 且 在输出但时候按考试号升序输出,如1、2、2、4。
思路
构造结构体person记录学生但考试号、成绩、最终排名、考场内排名、考场号;
每输入一个考场所有的信息就对其进行排序;
最后将所有考场的学生信息放入新的vector再次进行排序,并输出结果;
代码
#include<bits/stdc++.h>
using namespace std;
struct person{
string id;
int score,fin,local, number;
};
// 相同分数的人相同rank,一样就按照id递增输出
bool cmp(const person& a, const person& b){
return a.score == b.score ? a.id < b.id : a.score > b.score;
}
int main(int argc, const char * argv[]) {
int N, K, ans=0;
string id;
cin>>N;
vector<person> v[N+1];
for(int i=0; i<N; i++){
cin>>K;
ans+=K;
v[i].resize(K);
for(int j=0; j<K; j++){
cin>>v[i][j].id>>v[i][j].score;
v[i][j].number = i+1;
}
sort(v[i].begin(), v[i].end(), cmp);
// local rank
for(int j=0; j<K; j++){
if(j != 0 && v[i][j].score == v[i][j-1].score)
v[i][j].local = v[i][j-1].local;
else v[i][j].local = j+1;
v[N].push_back(v[i][j]);
}
}
sort(v[N].begin(), v[N].end(), cmp);
cout<<ans<<endl;
for(int j=0; j<ans; j++){
if(j != 0 && v[N][j].score == v[N][j-1].score)
v[N][j].fin = v[N][j-1].fin;
else v[N][j].fin = j+1;
printf("%s %d %d %d\n",v[N][j].id.c_str(), v[N][j].fin, v[N][j].number, v[N][j].local);
}
return 0;
}