1 条题解
-
0
练71.1成绩排序
解题思路
结构体或pair保存姓名与成绩,按成绩降序,成绩相同时姓名升序。逐条输出姓名和原成绩。
正确性说明
依题目给出的对应关系逐项处理,位置与原输入保持明确对应。每一步完成当前项的全部贡献,结束后得到所有位置或字符的目标结果。
易错点
同分不按输入顺序,而按名字字典序。
复杂度
姓名最长L,时间O(nL log n),空间O(nL)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<pair<string, int>> a(n); for (auto &p : a) cin >> p.first >> p.second; sort(a.begin(), a.end(), [](const pair<string, int> &x, const pair<string, int> &y) { if (x.second != y.second) return x.second > y.second; return x.first < y.first; }); for (auto &p : a) cout << p.first << " " << p.second << "\n"; return 0; }
- 1