29行中把string全部转成lowercase, 我记得之前写成
transform(school.begin(), school.end(), ::tolower);
是可行的,不知道为什么这次编译不过,改成下面这样才行。迷惑不解。
transform(school.begin(), school.end(), school.begin(), tolower);
整体代码如下:
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
int N, score, cnt, rank;
std::string id, school;
std::unordered_map<std::string, int> m;
struct sch{
std::string schCode;
double tws = 0.0;
int Ns = 0;
sch(std::string _str): schCode(_str){}
};
std::vector<sch> res;
bool cmp(const sch &a, const sch &b){
return (int)a.tws != (int)b.tws ? (int)a.tws > (int)b.tws : (a.Ns != b.Ns ? a.Ns < b.Ns : a.schCode < b.schCode);
}
int main(){
std::cin >> N;
cnt = 0;
for(int i = 0; i < N; ++i){
std::cin >> id >> score >> school;
transform(school.begin(), school.end(), school.begin(), tolower);
if(m.find(school) == m.end()){
m[school] = cnt++;
res.push_back(sch(school));
}
++res[m[school]].Ns;
if(id[0] == 'B'){
res[m[school]].tws += score / 1.5;
}else if(id[0] == 'A'){
res[m[school]].tws += score;
}else{
res[m[school]].tws += 1.5 * score;
}
}
std::cout << res.size() << std::endl;
sort(res.begin(), res.end(), cmp);
rank = 1;
std::cout << rank << " " << res[0].schCode << " " << (int)res[0].tws << " " << res[0].Ns << std::endl;
for(int i = 1; i < res.size(); ++i){
if((int)res[i].tws != (int)res[i - 1].tws){
rank = i + 1;
}
std::cout << rank << " " << res[i].schCode << " " << (int)res[i].tws << " " << res[i].Ns << std::endl;
}
return 0;
}
题目如下:
After each PAT, the PAT Center will announce the ranking of institutions based on their students' performances. Now you are asked to generate the ranklist.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤105), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:
ID Score School
where ID
is a string of 6 characters with the first one representing the test level: B
stands for the basic level, A
the advanced level and T
the top level; Score
is an integer in [0, 100]; and School
is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that ID
is unique for each testee.
Output Specification:
For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:
Rank School TWS Ns
where Rank
is the rank (start from 1) of the institution; School
is the institution code (all in lower case); ; TWS
is the total weighted score which is defined to be the integer part of ScoreB/1.5 + ScoreA + ScoreT*1.5
, where ScoreX
is the total score of the testees belong to this institution on level X
; and Ns
is the total number of testees who belong to this institution.
The institutions are ranked according to their TWS
. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns
. If there is still a tie, they shall be printed in alphabetical order of their codes.