题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
#include <algorithm> #include <iostream> #include <bits/stdc++.h> #include <utility> #include <vector> using namespace std; int main() { int n; cin >> n; int flag; cin >> flag; vector<pair<string, pair<int, int>>> data(n); for (int i = 0; i < n; ++i) { string name; int score; cin >> name >> score; data[i] = {name, {score, i}}; } if (flag) { sort(begin(data), end(data), [](pair<string, pair<int, int>>& a, pair<string, pair<int, int>>& b){return a.second.first != b.second.first ? a.second.first < b.second.first : a.second.second < b.second.second;}); } else { sort(begin(data), end(data), [](pair<string, pair<int, int>>& a, pair<string, pair<int, int>>& b){return a.second.first != b.second.first ? a.second.first > b.second.first : a.second.second < b.second.second;}); } for (const auto &[name, p] : data) { cout << name << " " << p.first << endl; } } // 64 位输出请用 printf("%lld")