思路清晰很重要
#include<iostream> // 自己写的,耗时很长
#include<unordered_map>
#include<vector>
#include<algorithm>
#include<cctype>
using namespace std;
struct Stu{
string id;
int score;
};
struct Ins{
int order, total, num;
string ins;
};
int n;
unordered_map<string, vector<Stu>> mp; // 保存机构中的学生
unordered_map<string, Ins> mp2; // 保存机构中的具体数据,如总分,排名,人数
vector<Ins> v;
bool cmp(const Ins& a, const Ins& b) {
return a.total != b.total ? a.total > b.total : (a.num != b.num ? a.num < b.num : a.ins < b.ins);
}
int main() {
cin >> n;
string a, c;
int b;
for(int i = 0; i < n; i++) {
cin >> a >> b >> c;
for(int i = 0; i < c.size(); i++) if(isupper(c[i])) c[i] = tolower(c[i]);
mp[c].push_back({a, b});
}
int mpSize = mp.size();
cout << mpSize << endl;
for(auto it : mp) {
vector<Stu> temp = it.second;
int scoreA = 0, scoreB = 0, scoreT = 0, tSize, totalW;
tSize = temp.size();
for(int i = 0; i < tSize; i++) {
if(temp[i].id[0] == 'A') scoreA += temp[i].score;
else if(temp[i].id[0] == 'B') scoreB += temp[i].score;
else scoreT += temp[i].score;
}
totalW = scoreB / 1.5 + scoreA + scoreT * 1.5;
Ins institude = {0, totalW, tSize, it.first};
v.push_back(institude);
}
sort(v.begin(), v.end(), cmp);
v[0].order = 1;
for(int i = 1; i < v.size(); i++) {
if(v[i].total != v[i-1].total) v[i].order = i + 1;
else v[i].order = v[i - 1].order;
}
for(auto it : v) {
cout << it.order << " " << it.ins << " " << it.total << " " << it.num << endl;
}
return 0;
}
柳神代码
#include<iostream>
#include<unordered_map>
#include<vector>
#include<algorithm>
#include<cctype>
using namespace std;
struct node{
string school;
int tws, ns;
};
int n;
unordered_map<string, int> cnt, sum;
vector<node> ins;
bool cmp(const node& a, const node& b) {
return a.tws != b.tws ? a.tws > b.tws : (a.ns != b.ns ? a.ns < b.ns : a.school < b.school);
}
int main() {
cin >> n;
string a, c;
int b;
for(int i = 0; i < n; i++) {
double scTotal = 0;
cin >> a >> b >> c;
for(int i = 0; i < c.size(); i++) c[i] = tolower(c[i]);
if(a[0] == 'A') {
scTotal += b;
} else if(a[0] == 'B') {
scTotal += b / 1.5;
} else if(a[0] == 'T') {
scTotal += b * 1.5;
}
sum[c] += scTotal;
cnt[c] ++;
}
for(auto it : cnt)
ins.push_back({it.first, (int)sum[it.first], it.second});
sort(ins.begin(), ins.end(), cmp);
cout << ins.size() << endl;
int rank, pres= -1;
for(int i = 0; i < ins.size(); i++) {
if(pres != ins[i].tws) rank = i+1;
pres = ins[i].tws; // 这种处理排名的方式值得熟记
printf("%d %s %d %d\n", rank, ins[i].school.c_str(), ins[i].tws, ins[i].ns);
}
return 0;
}