1、待排序中的元素作数组的下标或map的键值
例题:PAT甲级_1141 PAT Ranking of Institutions
#include<bits/stdc++.h>
using namespace std;
const int maxn = ; struct Node {
int personNum = ;
int scoreB = , scoreA = , scoreT = ;
int score;
}; map<string, Node> mp;
vector<string> ansSchoolName; bool cmp(string a, string b) {
if (mp[a].score != mp[b].score) return mp[a].score > mp[b].score;
else return mp[a].personNum < mp[b].personNum;
}
int main() {
int n;
scanf("%d", &n);
int score;
string id, schoolName;
for (int i = ; i < n; i++) {
cin >> id >> score >> schoolName;
transform(schoolName.begin(), schoolName.end(), schoolName.begin(), ::tolower);
mp[schoolName].personNum++;
if (id[] == 'A') mp[schoolName].scoreA += score;
else if (id[] == 'B') mp[schoolName].scoreB += score;
else mp[schoolName].scoreT += score;
}
for (auto it = mp.begin(); it != mp.end(); it++) {
ansSchoolName.push_back(it->first);
it->second.score = (int)(it->second.scoreA + it->second.scoreB / 1.5 + it->second.scoreT*1.5);
}
sort(ansSchoolName.begin(), ansSchoolName.end(), cmp);
int r = ;
printf("%d", ansSchoolName.size());
for (int i = ; i < ansSchoolName.size(); i++) {
if (i > && mp[ansSchoolName[i]].score != mp[ansSchoolName[i - ]].score) {
r = i + ;
}
printf("%d %s %d %d\n", r, ansSchoolName[i].c_str(), mp[ansSchoolName[i]].score, mp[ansSchoolName[i]].personNum);
}
return ; }