1025 PAT Ranking (25 分)[排序(小範圍和大範圍排序)]
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個local,每個local有k個考生,我們最後要將每個考生按照最後排名的順序輸出id、最後排名、local號及local中的排名。
解題思路:毫不猶豫用了結構,在main函式中首先定義了最後答案的vectorfinal,然後慢慢處理每個local的排名,每一個local處理完就將這個local的vector的資料壓入final中,直至所有的local的vector元素都壓入final中,最後再處理每個學員總的排名。這裡列印有個非常需要注意的點是要保證id13位輸出,不足13位用0填充。
#include<bits/stdc++.h>
using namespace std;
struct Rank{
long long id;
int grade,finalrank,local,localrank;
};
bool cmp(Rank a,Rank b)
{
if(a.grade==b.grade) return a.id<b.id;
return a.grade>b.grade;
}
int main(void)
{
int n,k,l=1;
scanf("%d",&n);
vector<Rank>final;//最後答案的vector
for(int j=1;j<=n;j++)
{
scanf("%d",&k);
vector<Rank>v(k);//local的vector
for(int i=0;i<k;i++)
{
scanf("%lld %d",&v[i].id,&v[i].grade);
}
sort(v.begin(),v.end(),cmp);
v[0].localrank=1;v[0].local=j;
for(int i=1;i<k;i++)
{
v[i].localrank=i+1;
if(v[i].grade==v[i-1].grade) v[i].localrank=v[i-1].localrank;
v[i].local=j;
}
for(int i=0;i<k;i++) final.push_back(v[i]);
}
sort(final.begin(),final.end(),cmp);
cout<<final.size()<<endl;//這裡沒看清輸出的資料,落掉了
final[0].finalrank=1;
printf("%013lld %d %d %d\n", final[0].id, final[0].finalrank, final[0].local, final[0].localrank);
for(int i=1;i<final.size();i++)
{
final[i].finalrank=i+1;
if(final[i].grade==final[i-1].grade) final[i].finalrank=final[i-1].finalrank;
// cout<<final[i].id<<" "<<final[i].finalrank<<" "<<final[i].local<<" "<<final[i].localrank<<endl;沒有控制輸出格式,要保證id13位輸出
printf("%013lld %d %d %d\n", final[i].id, final[i].finalrank, final[i].local, final[i].localrank);
}
return 0;
}