1025 PAT Ranking(模拟、排序)

 方法一:先对总榜按要求进行排序,再遍历总榜时持续维护绝对排名和相对排名并输出即可

 方法二:结构体中包含本地排名,在每输入一个测试点的数据以后就进行局部排序,得到本地排名,再将局部信息push到总榜中,再对总榜进行排序,直接输出即可。

方法一需要多开三个数组来维护本地排名信息,空间复杂度高了,但时间复杂度要比方法二低很多

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int n, k;
 4 vector<int> local_rank(105, 0); //本地绝对排名
 5 vector<int> relative_local_rank(105, 1); //本地相对排名
 6 vector<int> former_sorce(105, -1); //本地上一分数
 7 struct testees{
 8     string id;
 9     int sorce;
10     int location;
11 }test[30005];
12 bool cmp(const testees &t1, const testees &t2) {
13     if (t1.sorce == t2.sorce) {
14         return t1.id < t2.id;
15     }
16     return t1.sorce > t2.sorce;
17 }
18 int main() {
19     cin >> n;
20     int index = 0;
21     for (int i = 1; i <= n; ++ i) {
22         cin >> k;
23         for (int j = 0; j < k; ++ j, ++ index) {
24             string num;
25             cin >> test[index].id >> test[index].sorce;
26             test[index].location = i;
27         }
28         
29     }
30     cout << index << endl;
31     sort(test, test + index, cmp);
32     int relative_rank = 1; //总排行相对排名
33     for (int i = 0; i < index; ++ i) { //i + 1就是总绝对排名
34         if (i > 0) {
35             if (test[i].sorce != test[i - 1].sorce) { //分数不同更新总相对排名
36                 relative_rank = i + 1;
37             }
38         }
39         int location = test[i].location;
40         local_rank[location] ++; //更新本地绝对排名
41         if (former_sorce[location] != test[i].sorce) { //更新本地相对排名
42             former_sorce[location] = test[i].sorce;
43             relative_local_rank[location] = local_rank[location];
44         }
45         cout << test[i].id << " " << relative_rank << " " << location << " " << relative_local_rank[location] << endl;
46     }
47 }

 

posted on 2024-11-21 20:30  Coder何  阅读(13)  评论(0)    收藏  举报