1141 PAT Ranking of Institutions (25 分)

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 (≤10​^5​​ ), 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.

Sample Input:
10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu
Sample Output:
5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

题目大意:
给定n个学生的id、score、school,学校名称不区分大小写。要求输出每个学校的排名、加权总分和学生人数

算法分析:
设置结构体存储每个学生的信息,将出现的学校名称单独放入一个set中(去重)。输入完毕后,遍历set,计算每个学校的总分和学生人数,另设结构体压入vector中,再对vector进行排序

错误代码(20分,最后两个点运行超时)

#include<bits/stdc++.h>
using namespace std;
struct testee{string id;int score;string school;
};
struct testee2{string school;int totalScore;int cnt;
};
int cmp(testee2 a,testee2 b){if(a.totalScore!=b.totalScore)return a.totalScore>b.totalScore;if(a.cnt!=b.cnt)return a.cnt<b.cnt;return a.school<b.school;
}
int main()
{int num;cin>>num;struct testee list[num];set<string> s;vector<testee2> v;for(int i=0;i<num;i++){getchar();cin>>list[i].id>>list[i].score>>list[i].school;for(int j=0;j<list[i].school.size();j++){if(list[i].school[j]>='A'&&list[i].school[j]<='Z')list[i].school[j]=tolower(list[i].school[j]);}s.insert(list[i].school);}for(auto it=s.begin();it!=s.end();it++){int total=0,cnt=0;for(int j=0;j<num;j++){if(list[j].school==*it){cnt++;if(list[j].id[0]=='B')total+=list[j].score/1.5;else if(list[j].id[0]=='A')total+=list[j].score;elsetotal+=list[j].score*1.5;}}testee2 t={*it,total,cnt};v.push_back(t);}sort(v.begin(),v.end(),cmp);int cnt=1,front=v[0].totalScore;cout<<v.size()<<"\n";cout<<cnt<<" "<<v[0].school<<" "<<v[0].totalScore<<" "<<v[0].cnt<<"\n"; for(int i=1;i<v.size();i++){if(v[i].totalScore==front)//cnt中存储的是输出的前一个学校的排名,如果二者总分相同则排名一样 cout<<cnt<<" "<<v[i].school<<" "<<v[i].totalScore<<" "<<v[i].cnt<<"\n"; else{cout<<i+1<<" "<<v[i].school<<" "<<v[i].totalScore<<" "<<v[i].cnt<<"\n"; cnt=i+1;}front=v[i].totalScore;}
}

后参考柳神代码,设置两个map,cnt用来计算学校的学生人数,sum用来计算总分。在输入时直接完成对学生人数和总分的计算。输入完毕后,遍历任意map,将学校名称、分数、学生人数信息放入结点中,再压入vector,最后对vector进行排序

注意:
对于输出的处理:设立front设立上一个学校的总分,当当前学校的总分和front相同时,二者排名相同;若不同,排名即为遍历的顺序

读入的score要用double存储,不能直接用int

AC代码:

#include<bits/stdc++.h>
using namespace std;
struct testee2{string school;int totalScore;int cnt;
};
int cmp(testee2 a,testee2 b){if(a.totalScore!=b.totalScore)return a.totalScore>b.totalScore;if(a.cnt!=b.cnt)return a.cnt<b.cnt;return a.school<b.school;
}
int main()
{int num;double score;string id,school;cin>>num;vector<testee2> v;map<string,int> cnt;map<string,double> sum;for(int i=0;i<num;i++){getchar();//cin会将回车放入缓冲区中,记得读掉 cin>>id>>score>>school;for(int j=0;j<school.size();j++){if(school[j]>='A'&&school[j]<='Z')school[j]=tolower(school[j]);}if(id[0]=='B')score=score/1.5;//这里的score一定要用double存,不能每次计算完后直接转成int,不然最后一个点过不了 else if(id[0]=='T')score=score*1.5;sum[school]+=score;cnt[school]++;}for(auto it=sum.begin();it!=sum.end();it++){v.push_back(testee2{it->first,(int)it->second,cnt[it->first]});}sort(v.begin(),v.end(),cmp);int cnt2=1,front=v[0].totalScore;cout<<v.size()<<"\n";cout<<cnt2<<" "<<v[0].school<<" "<<v[0].totalScore<<" "<<v[0].cnt<<"\n"; for(int i=1;i<v.size();i++){if(v[i].totalScore==front)//cnt2中存储的是输出的前一个学校的排名,如果二者总分相同则排名一样 cout<<cnt2<<" "<<v[i].school<<" "<<v[i].totalScore<<" "<<v[i].cnt<<"\n"; else{cout<<i+1<<" "<<v[i].school<<" "<<v[i].totalScore<<" "<<v[i].cnt<<"\n"; cnt2=i+1;}front=v[i].totalScore;}
}

1141 PAT Ranking of Institutions (25 分)相关推荐

  1. PAT(A) - 1141. PAT Ranking of Institutions (25)

    1141. PAT Ranking of Institutions (25) 时间限制 500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  2. 1141. PAT Ranking of Institutions (25)

    https://github.com/uiiuiiu/PAT/blob/master/Advanced/1141%20%20Ranking%20of%20Institutions%EF%BC%8825 ...

  3. PAT甲级1141 PAT Ranking of Institutions :[C++题解]结构体、排序、哈希表、结构体构造函数、结构体内写函数、排名

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:和下面这题是一道题: PAT甲级1137 Final Grading:[C++题解]结构体.排序.哈希表.结构体构造函数.结构体内写函 ...

  4. 17冬第二题 PAT甲级 1141 Ranking of Institutions (25分) 有点鸡贼

    题目 After each PAT, the PAT Center will announce the ranking of institutions based on their students' ...

  5. 1141 PAT Ranking of Institutions (PAT甲级)

    29行中把string全部转成lowercase, 我记得之前写成 transform(school.begin(), school.end(), ::tolower); 是可行的,不知道为什么这次编 ...

  6. PAT A1141 PAT Ranking of Institutions ——昨夜西风凋碧树

    PAT A1141 PAT Ranking of Institutions 中间计算TWS时不能使用int,否则最后一个测试点不过.但是比较和输出又需要用int,在里面强转int也不给AC,无奈只好先 ...

  7. PAT 1110 区块反转 (25 分) c语言

    链表题欸,继续偷懒就可以了.需要注意一下最后一个测试点,其中存在链表以外的节点,因此链表长度需要重新计算. 题目: 1110 区块反转 (25 分) 给定一个单链表 L,我们将每 K 个结点看成一个区 ...

  8. PAT | 1025 反转链表 (25分)【超时问题 + 柳神代码】

    1025 反转链表 (25分) 给定一个常数 K 以及一个单链表 L,请编写程序将 L 中每 K 个结点反转.例如:给定 L 为 1→2→3→4→5→6,K 为 3,则输出应该为 3→2→1→6→5→ ...

  9. 19年冬季第二题 PAT甲级 1166 Summit (25分)

    7-3 Summit (25分) A summit (峰会) is a meeting of heads of state or government. Arranging the rest area ...

最新文章

  1. Web开发的标准目录结构
  2. Git 自救指南:这些坑你都跳得出吗?
  3. mac brew 安装
  4. 正则不以什么开头_python基础 | 正则扫盲
  5. 精彩回顾 | Dapr闪电说系列
  6. 工作总结21:阅读代码之axios
  7. 700 页的机器学习笔记火啦,图文生动形象
  8. webpack 合并压缩_webpack 打包压缩js和css的方法示例
  9. 趣文:舌尖上的程序猿
  10. 以Crypto++实现RSA加解密二进制数据
  11. 如何配置SQL Server ODBC数据源
  12. Python:常见排列组合问题处理
  13. 计算机专业纸质笔记本,无可替代?信息时代你还用纸质笔记本吗
  14. 卷毛机器人抢大龙_EDG卷毛宣布退役:感谢WE和EDG的培养
  15. 你喜欢最华为手机哪一点?网友评论亮了
  16. 关键链方法的多项目监控技术
  17. 一个微信可以有多个头像昵称了
  18. JAVA基础语法笔记(黑马程序员系列)
  19. 利用imu估计roll、pitch的理解
  20. 【经典之作】松岩老师打底十二法!前言

热门文章

  1. C++语言之vector用法
  2. 记录参加CSDN上海大联欢活动
  3. SolidWorks 2019鼠标基本操作——模型控制操作
  4. 博士申请 | 新加坡国立大学Robby T. Tan教授招收CV方向全奖博士/博后/访问学生
  5. 在Qt中制作入门动画-1
  6. Linux设置文件和目录权限
  7. [RTOS--Structure]实时操作系统中ISR的处理为何要精短
  8. scp传文件指定端口
  9. 《程序员》2012年5期精彩内容:云计算!
  10. js返回上一页并刷新的几种方法