题解 | #奥运排序问题#
奥运排序问题
https://www.nowcoder.com/practice/100a4376cafc439b86f5f8791fb461f3
#include <bits/stdc++.h> #define Rank pair<int,int> // <rank,rank type> using namespace std; using Country = class Node { public: int Golds, Medals, Population, Order; double GoldRatio, MedalRatio; vector<Rank> ranks; Node(int g, int m, int p, int o) : Golds(g), Medals(m), Population(p), Order(o) { GoldRatio = (Population != 0) ? ((double)Golds / Population) : (Golds == 0 ? 0 : INT_MAX); MedalRatio = (Population != 0) ? ((double)Medals / Population) : (Medals == 0 ? 0 : INT_MAX); } void setRank(int rank, int type) { ranks.emplace_back(rank, type); } }; bool compareGolds(Country c1, Country c2) { return c1.Golds > c2.Golds; } bool compareMedals(Country c1, Country c2) { return c1.Medals > c2.Medals; } bool compareGoldRatio(Country c1, Country c2) { return c1.GoldRatio > c2.GoldRatio; } bool compareMedalRatio(Country c1, Country c2) { return c1.MedalRatio > c2.MedalRatio; } bool compareRank(Rank r1, Rank r2) { if (r1.first != r2.first) return r1.first < r2.first; return r1.second < r2.second; } bool compareOrder(Country c1, Country c2) { return c1.Order < c2.Order; } int main() { int nTotal, mCountry, g, m, p, id; cin >> nTotal >> mCountry; vector<Country> countries; for (int i = 0; i < nTotal; i++) { cin >> g >> m >> p; countries.emplace_back(g, m, p, i); } sort(countries.begin(), countries.end(), compareGolds); for (int i = 0; i < nTotal;) { int data = countries[i].Golds, rank = i + 1; while (i < nTotal && countries[i].Golds == data) countries[i++].setRank(rank, 1); } sort(countries.begin(), countries.end(), compareMedals); for (int i = 0; i < nTotal;) { int data = countries[i].Medals, rank = i + 1; while (i < nTotal && countries[i].Medals == data) countries[i++].setRank(rank, 2); } sort(countries.begin(), countries.end(), compareGoldRatio); for (int i = 0; i < nTotal;) { double data = countries[i].GoldRatio, rank = i + 1; while (i < nTotal && countries[i].GoldRatio == data) countries[i++].setRank(rank, 3); } sort(countries.begin(), countries.end(), compareMedalRatio); for (int i = 0; i < nTotal;) { double data = countries[i].MedalRatio, rank = i + 1; while (i < nTotal && countries[i].MedalRatio == data) countries[i++].setRank(rank, 4); } sort(countries.begin(), countries.end(), compareOrder); for (int i = 0; i < mCountry; i++) { cin >> id; sort(countries[id].ranks.begin(), countries[id].ranks.end(), compareRank); cout << countries[id].ranks[0].first << ":" << countries[id].ranks[0].second << endl; } return 0; }
六连排序