1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #include<iostream> #include<algorithm> #include<string>
using namespace std; const int N = 110; int n, x;
void minTomax(vector<pair<string, int>>& arr){ stable_sort(arr.begin(), arr.end(), [&](const pair<string, int> &a, const pair<string, int> &b){ return a.second < b.second; }); } void maxTomin(vector<pair<string, int>>& arr){ stable_sort(arr.begin(), arr.end(), [&](const pair<string, int> &a, const pair<string, int> &b){ return a.second > b.second; }); }
int main(){ cin >> n; cin >> x; vector<pair<string, int>> arr; for(int i = 0; i < n; i++){ string a; int b; cin >> a >> b; arr.emplace_back(make_pair(a, b)); } if(x == 0) maxTomin(arr); else minTomax(arr); for(int i = 0; i < arr.size(); i++){ cout << arr[i].first << " " << arr[i].second << endl; } return 0; }
|