PAT-ADVANCED1025——PAT Ranking
阿新 • • 發佈:2018-12-19
題目描述:
題目翻譯:
1025 PAT排名
程式設計能力測試(PAT)由浙江大學電腦科學與技術學院組織。 每個測試應該在幾個地方同時執行,並且排名列表將在測試後立即合併。 現在,你的工作是編寫一個程式來正確合併所有排名並生成最終排名。
輸入格式:
每個輸入檔案包含一個測試用例。對每個測試用例,第一行包含一個正整數N(<= 100),代表考場數量。接下來的N個考場資訊,每個考場資訊以一個正整數K(<= 300)開頭,代表這個考場的考生總數,接下來的K行,每行包含一個13位的准考證號和該考生的分數。一行中的所有數字由一個空格分隔。
輸出格式:
對每個測試用例,在第一行中打印出考生總數。然後以下述形式輸出最終排名列表:
registration_number final_rank location_number local_rank
考場編號從1到N。輸出必須按照final_rank的非降序排列。擁有相同分數的考生有相同的排名且按registration_number的非降序排列。
輸入樣例:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
輸出樣例:
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
知識點:排序
思路:先對每個考場的考生進行排序得到考生的考場排名,再對所有考生進行排序得到考生的總排名
用一個結構體student儲存考生編號、成績、考場號、最終排名、考場排名這5個資訊。
時間複雜度是O(nlogn),其中n為總考生數。空間複雜度是O(n)。
C++程式碼:
#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; struct student { string number; int score; int final_rank; int location_number; int local_rank; }; int N; vector<student> students[101]; vector<student> allStudents; bool cmp(student stu1, student stu2); int main() { scanf("%d", &N); int K; string number; int score; student stu; for(int i = 1; i <= N; i++) { scanf("%d", &K); for(int j = 0; j < K; j++) { cin >> number >> score; stu.number = number; stu.score = score; stu.location_number = i; students[i].push_back(stu); } } for(int i = 1; i <= N; i++) { sort(students[i].begin(), students[i].end(), cmp); } for(int i = 1; i <= N; i++) { for(int j = 0; j < students[i].size(); j++) { if(j > 0 && students[i][j].score == students[i][j - 1].score) { students[i][j].local_rank = students[i][j - 1].local_rank; } else { students[i][j].local_rank = j + 1; } allStudents.push_back(students[i][j]); } } sort(allStudents.begin(), allStudents.end(), cmp); for(int i = 0; i < allStudents.size(); i++) { if(i > 0 && allStudents[i].score == allStudents[i - 1].score) { allStudents[i].final_rank = allStudents[i - 1].final_rank; } else { allStudents[i].final_rank = i + 1; } } cout << allStudents.size() << endl; for(int i = 0; i < allStudents.size(); i++){ cout << allStudents[i].number << " " << allStudents[i].final_rank << " " << allStudents[i].location_number << " " << allStudents[i].local_rank << endl; } } bool cmp(student stu1, student stu2) { if(stu1.score == stu2.score) { return stu2.number.compare(stu1.number) > 0; } else { return stu1.score > stu2.score; } }
C++解題報告: