PAT A1141 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 (≤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.
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
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <set>
#include <cctype>
using namespace std;
const int maxn=;
int n,m,k;
struct node{
string id;
int score;
string ins;
};
struct inst{
string insti;
int tws;
int score[]={,,};
int ns;
bool operator < (const inst& a)const{
if(tws>a.tws)return true;
else if(tws==a.tws){
if(ns<a.ns) return true;
else if(ns==a.ns){
return insti<a.insti?true:false;
}
else return false;
}
else return false;
}
};
int cnt;
map<string,inst> mp;
vector<inst> v;
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
string id,ins;
int score,tw=;
cin>>id>>score>>ins;
transform(ins.begin(), ins.end(), ins.begin(), ::tolower);
if(mp.find(ins)==mp.end()){
inst tmp;
tmp.insti=ins;
tmp.ns=;
//tmp.tws=tw;
if(id[]=='B'){
tmp.score[]=score;
}
else if(id[]=='A'){
tmp.score[]=score;
}
else{
tmp.score[]=score;
}
mp[ins]=tmp;
}
else{
inst tmp=mp[ins];
tmp.ns++;
if(id[]=='B'){
tmp.score[]+=score;
}
else if(id[]=='A'){
tmp.score[]+=score;
}
else{
tmp.score[]+=score;
}
mp[ins]=tmp;
}
}
printf("%d\n",mp.size());
int rank=;
for(auto it=mp.begin();it!=mp.end();it++){
inst tmp=it->second;
tmp.tws=tmp.score[]/1.5+tmp.score[]+tmp.score[]*1.5;
v.push_back(tmp);
}
sort(v.begin(),v.end());
printf("%d %s %d %d\n",rank,v[].insti.c_str(),v[].tws,v[].ns);
for(int i=;i<v.size();i++){
if(v[i].tws!=v[i-].tws) rank=i+;
printf("%d %s %d %d\n",rank,v[i].insti.c_str(),v[i].tws,v[i].ns);
}
}
注意点:排序题有sort的帮助还是比较简单的,虽然还是花了很久,调试了很久。还用到了map和vector,最开始想直接map里面排序,还写了结构体重载函数,发现map是根据第一个键值来排序的,那只好再保存到vector里,反正也要遍历一遍算tws。重载了<号就不用写cmp函数了。因为set和map自动排序是根据小于号判断两个结构体的大小,小的放前面,sort 排序不写cmp函数也是自动根据小于号有小到大排列。
第一个坑是最开始每读一个分数看他哪个等级就更新 tws,这样最后一个测试点答案错误,以为要用float,重新看了遍题,他是取整数部分,但不是每一个来取,而是对所有对应等级的总和取整,这就还需要保存一个总和值最后一起来算
第二个坑是结构体里面保存总和的数组没有显式初始化为0,算出来答案一直错误,需要手动初始化为0,就AC了
第三个注意点是字符串转大小写,用到 algorithm 里的 transform 函数,格式要记住
PAT A1141 PAT Ranking of Institutions (25 分)——排序,结构体初始化的更多相关文章
- PAT A1075 PAT Judge (25 分)——结构体初始化,排序
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...
- PAT 甲级 1012 The Best Rank (25 分)(结构体排序)
题意: 为了评估我们第一年的CS专业学生的表现,我们只考虑他们的三个课程的成绩:C - C编程语言,M - 数学(微积分或线性代数)和E - 英语.同时,我们鼓励学生强调自己的最优秀队伍 - 也就是说 ...
- 【PAT甲级】1016 Phone Bills (25 分)(结构体排序)
题意: 输入24个正整数代表从0到23每个小时通话一分钟花费的美分.输入一个正整数N(<=1000),然后输入N组字符串,每个字符串包含客户的名字和通话的时刻以及打出或者挂断的状态. 按照字典序 ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)
1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following r ...
- PAT 甲级1025 PAT Ranking (25 分)(结构体排序,第一次超时了,一次sort即可小技巧优化)
题意: 给定一次PAT测试的成绩,要求输出考生的编号,总排名,考场编号以及考场排名. 分析: 题意很简单嘛,一开始上来就,一组组输入,一组组排序并记录组内排名,然后再来个总排序并算总排名,结果发现最后 ...
- 【PAT甲级】1025 PAT Ranking (25 分)(结构体排序,MAP<string,int>映射)
题意: 输入一个正整数N(N<=100),表示接下来有N组数据.每组数据先输入一个正整数M(M<=300),表示有300名考生,接下来M行每行输入一个考生的ID和分数,ID由13位整数组成 ...
- PAT (Advanced Level) Practice 1055 The World's Richest (25 分) (结构体排序)
Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...
- 【PAT甲级】1026 Table Tennis (30 分)(结构体排序,trick较多)
题意: 输入一个正整数N(<=10000),表示客户(对)的大小,接着输入N行数据,每行包括一对顾客到场的时间,想要玩的时间,以及是否是VIP客户.接下来输入两个正整数K,M(K<=100 ...
- PAT乙级:1090危险品装箱(25分)
PAT乙级:1090危险品装箱(25分) 题干 集装箱运输货物时,我们必须特别小心,不能把不相容的货物装在一只箱子里.比如氧化剂绝对不能跟易燃液体同箱,否则很容易造成爆炸. 本题给定一张不相容物品的清 ...
随机推荐
- What is The Rule of Three?
Question: What does copying an object mean? What are the copy constructor and the copy assignment op ...
- EmitMapper的使用小结
最近公司开发项目前端使用一个js框架,后端使用ef,js前台读取的json采用实体的dto来进行生成. 在网上看到了EmitMapper相对其他映射框架处理速度可以更快,就拿来用了.下面是代码中常用的 ...
- js-Higher-base.js
// 1.基于原型链的继承 // 继承属性 // 当访问一个对象的属性时发生的行为: // 假定有一个对象 o, 其自身的属性(own properties)有 a 和 b: {a: 1, b: 2} ...
- Vue和React的对比
今晚我们来搞一搞Vue和React的对比好吧,话不多说今天我们直接开搞可好,各位小老板,开始吧 1. react整体是函数式的思想,把组件设计成纯组件,状态和逻辑通过参数传入, 所以在react中,是 ...
- 喜闻乐见-Android简介
本文主要是对Android系统做一个简介,包括其架构.启动流程.沙箱机制.APK.Darlvik以及ART. 1. 架构 Android是基于Linux内核开发出的一个移动操作系统,系统结构大致可以分 ...
- python中关于类隐藏属性的三种处理方法
关于隐藏属性 引子: 当类的属性或者类实例对象的属性隐藏的时候必须通过存取器方法来获取和设置这些隐藏的属性. 例如: def get_name(self,name): #存取器方法 self. ...
- maven(六),外置maven运行环境配置
外置maven eclipse内置的maven插件是固定版本,如果要用其他版本的maven,可以使用外置maven 下载地址: http://maven.apache.org/download.cgi ...
- c#所有部门及其下所部门生成树形图(递归算法获取或键值对方式获取)
部门数据库的设计: 代码: /// <summary> /// 获取部门(入口) /// </summary> /// <returns></returns& ...
- centos7安装rabbitmq 总结
centos7下安装rabbitmq 折腾了三天最后做了以下总结 先查看一电脑名 :示例 #hostname name 查看一下hosts配置文件:如果如下结果,就要修改下 #cat /etc/ho ...
- 为什么内核访问用户数据之前,要做access_ok?【转】
linuxer 案例 比如内核的如下commit引入了一个严重的安全漏洞(编号CVE-2017-5123): 危害 一个攻击案例可以参考: freebuf <Linux内核Waitid系统调用本 ...