1141 PAT Ranking of Institutions
中文版
1085 PAT单位排行
无需构造函数,快速初始化结构体。
1 #include<iostream> 2 #include<cctype> 3 #include<vector> 4 #include<unordered_map> 5 #include<algorithm> 6 using namespace std; 7 8 struct School { 9 string name; 10 int tws,ns; 11 }; 12 13 bool cmp(const School &a,const School &b) { 14 if(a.tws != b.tws) return a.tws > b.tws; 15 if(a.ns != b.ns) return a.ns < b.ns; 16 return a.name < b.name; 17 } 18 int main() { 19 int n; 20 cin>>n; 21 string id,school; 22 double score; 23 unordered_map<string,double> mp; 24 unordered_map<string,int> num; 25 for(int i = 0; i < n; ++i) { 26 cin>>id>>score>>school; 27 for(int j = 0; j < school.size(); ++j) school[j] = tolower(school[j]); 28 if(id[0] == 'B') score /= 1.5; 29 else if(id[0] == 'T') score *= 1.5; 30 mp[school] += score; 31 num[school]++; 32 } 33 cout<<mp.size()<<endl; 34 vector<School> v; 35 for(auto it:mp) v.push_back({it.first,(int)it.second,num[it.first]}); 36 sort(v.begin(),v.end(),cmp); 37 int r = 1; 38 for(int i = 0; i < v.size(); ++i) { 39 if(i > 0 && v[i].tws != v[i-1].tws) 40 r = i+1; 41 printf("%d %s %d %d\n",r,v[i].name.c_str(),v[i].tws,v[i].ns); 42 } 43 return 0; 44 }