考察:复杂结构体排序+map的使用
说实话,,我觉得这题有点坑,菜鸡本来没有用map写,结果最后两个测试点过不去,改了map之后,发现要先用double记总分之后再转成int,不然的话最后一个测试点会答案错误。
附上代码:
#include <iostream>
#include <cctype>
#include <algorithm>
#include <map>
using namespace std;
struct node{
string school;
double w_score = 0;
int score;
int num = 0;
};
bool cmp(node &p, node &q){
if(p.score != q.score) return p.score>q.score;
else if(p.num != q.num) return p.num<q.num;
else return p.school<q.school;
}
node School[100010];
int main(){
int N, k = 0;
map<string, int> mp;
cin >> N;
while(N --){
string id, sch;
double score;
cin >> id >> score >> sch;
if(id[0] == 'T') score *= 1.5;
if(id[0] == 'B') score /= 1.5;
for(int i = 0; i < sch.size(); i ++){
sch[i] = tolower(sch[i]);
}
if(mp.find(sch) == mp.end()){
mp[sch] = k; //给每个学校一个下标值,方便存储,相当于一一对应
k ++;
}
School[mp[sch]].school = sch;
School[mp[sch]].num ++;
School[mp[sch]].w_score += score;
}
for(int i = 0; i < k; i ++){
School[i].score = (int)School[i].w_score;
}
sort(School, School+k, cmp);
printf("%d\n", k);
int r = 1;
for(int i = 0; i < k; i ++){
if(i > 0 && School[i].score != School[i-1].score){
r = i + 1;
}
printf("%d %s %d %d\n", r, School[i].school.c_str(), School[i].score, School[i].num);
}
return 0;
}
我觉得强制转换之后还是重新存一个变量比较好。。