看着人畜无害的一个题, 其实挺麻烦的
首先要考虑用map, 写了这题顺便复习一下map的各种知识了… 而由于map不能直接sort排序, 所以要考虑转存一下
因为这题数据比较大, 所以只能考虑用map, 而且发现即使用map也有一个点死活过不去…要用unordered_map
这是我自己的代码, 有一个点过不去
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <bits/unordered_map.h>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const LL maxn = 1e5+10;
struct node{
int cont, score;
string name;
};
bool cmp(const pair<string, node> &a, const pair<string, node> &b)
{
if(a.second.score != b.second.score)
return a.second.score > b.second.score;
else{
if(a.second.cont != b.second.cont)
return a.second.cont < b.second.cont;
else
return a.second.name < b.second.name; //字典序升序排列
}
}
unordered_map<string, node> s;
vector <pair<string, node> > ts;
int main()
{
char level, school[7];
int id, score, n;
cin >> n;
for(int i = 0; i < n; i++){
getchar();
scanf("%c%d %d %s",&level,&id,&score,school);
//转小写
for(int i = 0; i < strlen(school); i++)
school[i] = tolower(school[i]);
s[school].cont++, s[school].name = school;
if(level=='A') s[school].score+=score;
else if(level=='B') s[school].score+=(score/1.5);
else if(level=='T') s[school].score+=(score*1.5);
}
for(map<string, node>::iterator it = s.begin(); it != s.end(); it++)
ts.push_back(make_pair(it->first, it->second)); //转存vecto进行排序
sort(ts.begin(), ts.end(), cmp);
int i = 1, j = 1;
cout << ts.size() << endl;
for(vector<pair<string, node> >::iterator it = ts.begin(); it != ts.end(); j++){
printf("%d %s %d %d\n",i,it->second.name.c_str(),it->second.score,it->second.cont);
if(it->second.score != (++it)->second.score){ //考虑并列
if(i < j) i = j+1;
else i++;
}
}
return 0;
}
这是参考柳神的代码, 可以看到因为unordered_map因为要实现一系列函数很麻烦, 直接把值作为基本变量其实方便了一点
#include <iostream>
#include <algorithm>
#include <cctype>
#include <vector>
#include <unordered_map>
using namespace std;
struct node {
string school;
int tws, ns;
};
bool cmp(node a, node b) {
if (a.tws != b.tws)
return a.tws > b.tws;
else if (a.ns != b.ns)
return a.ns < b.ns;
else
return a.school < b.school;
}
int main() {
int n;
scanf("%d", &n);
unordered_map<string, int> cnt;
unordered_map<string, double> sum;
for (int i = 0; i < n; i++) {
string id, school;
cin >> id;
double score;
scanf("%lf", &score);
cin >> school;
for (int j = 0; j < school.length(); j++)
school[j] = tolower(school[j]);
if (id[0] == 'B')
score = score / 1.5;
else if (id[0] == 'T')
score = score * 1.5;
sum[school] += score;
cnt[school]++;
}
vector<node> ans;
for (auto it = cnt.begin(); it != cnt.end(); it++)
ans.push_back(node{it->first, (int)sum[it->first], cnt[it->first]});
sort(ans.begin(), ans.end(), cmp);
int rank = 0, pres = -1;
printf("%d\n", (int)ans.size());
for (int i = 0; i < ans.size(); i++) {
if (pres != ans[i].tws) rank = i + 1;
pres = ans[i].tws;
printf("%d ", rank);
cout << ans[i].school;
printf(" %d %d\n", ans[i].tws, ans[i].ns);
}
return 0;
}