当前位置: 首页 > news >正文

【PAT甲级】1141 PAT Ranking of Institutions

【PAT题解集合】

1141 PAT Ranking of Institutions

题目详情 - 1141 PAT Ranking of Institutions (pintia.cn)

中文翻译

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

思路

这道题需要对所有单位进行排序,输出的第一个值是排位;第二个值是单位名称;第三个值是该单位中所有人的分数权值之和,取整数;第四个值输出该单位的人数总和。

  1. 先将所有信息读入哈希表中,哈希表中的主键维护的是小写的字符串,所以要将大写字母全部转换成小写字母再存入哈希表。另外,哈希表中存的是计算后总分数,所以要对读取的分数进行换算,根据 id 中第一个字母进行改变,如果是 B 需要除以 1.5 ,如果是 A 直接加上就好,如果是 T 需要乘以 1.5
  2. 我们用一个 vector 容器存放哈希表中的所有单位信息,因为 vector 可以利用 sort 函数进行排序。当然这里的 sort 需要自定义排序规则,按照单位总分数的大小进行降序排序,如果分数相等则按照每个单位的总人数进行升序排序,如果单位人数也相等则按照单位名字的字典序从小到大进行排序。
  3. 排完序之后先输出总单位数,然后依次输出每个单位的排位信息,注意排名只根据分数进行划分,存在出现同排名的情况。

代码

#include<bits/stdc++.h>
using namespace std;

struct School {
    string name;
    int cnt;
    double sum;

    School() :cnt(0), sum(0) {}

    bool operator <(const School& s)const
    {
        if (sum != s.sum)  return sum > s.sum;   //按分数降序
        if (cnt != s.cnt)  return cnt < s.cnt;   //按人数升序
        return name < s.name; //按字典序升序
    }
};

int main()
{
    int n;
    cin >> n;

    unordered_map<string, School> hash;
    for (int i = 0; i < n; i++)
    {
        string id, name;
        double score;
        cin >> id >> score >> name;

        //将大写转换成小写
        for (auto& c : name)   c = tolower(c);

        //根据等级改变分数
        if (id[0] == 'B')  score /= 1.5;
        else if (id[0] == 'T') score *= 1.5;

        //更新信息
        hash[name].sum += score;
        hash[name].cnt++;
        hash[name].name = name;
    }

    //将所有单位存到容器中去,方便排序
    vector<School> ans;
    for (auto x : hash)
    {
        x.second.sum = (int)(x.second.sum + 1e-8); //防止出现精度问题
        ans.push_back(x.second);
    }
    sort(ans.begin(), ans.end());    //排序

    cout << ans.size() << endl; //输出有多少个单位

    //输出每所单位信息
    int rank = 1;
    for (int i = 0; i < ans.size(); i++)
    {
        auto x = ans[i];
        if (i > 0 && x.sum < ans[i - 1].sum)    rank = i + 1;
        printf("%d %s %d %d\n", rank, x.name.c_str(), (int)x.sum, x.cnt);
    }

    return 0;
}

相关文章:

  • JAVA基础(三十二)——反射之创建对象
  • java优秀毕业生推荐系统ssm
  • Group DETR
  • 设计模式 工厂方法模式
  • 自动控制原理7.3---z变换理论
  • 基于ISO14229协议的安全访问系列_1
  • 自动控制原理7.4---离散系统的数学模型
  • 【ELFK】之zookeeper
  • ALTERA FPGA IPCORE核之FIFO详细教程
  • 【ELK】日志分析系统概述及部署
  • MyBatis一对多查询,MyBatis中resultMap的使用,MyBatis中collection注意事项,MyBatis的级联搜索
  • STC15单片机-RS-485通信
  • 【JAVA-1】JDK、JRE安装及卸载,有手就会!
  • Python 操作MySql数据库(封装、优雅)
  • 《蓝海战略》让你竞争中获得优势
  • 2019年如何成为全栈工程师?
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • hadoop集群管理系统搭建规划说明
  • javascript数组去重/查找/插入/删除
  • LintCode 31. partitionArray 数组划分
  • Python连接Oracle
  • Redis中的lru算法实现
  • vagrant 添加本地 box 安装 laravel homestead
  • Windows Containers 大冒险: 容器网络
  • 小程序 setData 学问多
  • 协程
  • 1.Ext JS 建立web开发工程
  • ​ArcGIS Pro 如何批量删除字段
  • # 深度解析 Socket 与 WebSocket:原理、区别与应用
  • #{}和${}的区别是什么 -- java面试
  • #微信小程序:微信小程序常见的配置传值
  • (11)MATLAB PCA+SVM 人脸识别
  • (Java)【深基9.例1】选举学生会
  • (二)PySpark3:SparkSQL编程
  • (附源码)计算机毕业设计高校学生选课系统
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理第3章 信息系统治理(一)
  • (七)理解angular中的module和injector,即依赖注入
  • (转)【Hibernate总结系列】使用举例
  • (转)linux自定义开机启动服务和chkconfig使用方法
  • (转载)虚函数剖析
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .Net Core webapi RestFul 统一接口数据返回格式
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件
  • .net流程开发平台的一些难点(1)
  • .NET牛人应该知道些什么(2):中级.NET开发人员
  • @media screen 针对不同移动设备
  • [2018/11/18] Java数据结构(2) 简单排序 冒泡排序 选择排序 插入排序
  • [C# 网络编程系列]专题六:UDP编程
  • [C/C++]_[初级]_[关于编译时出现有符号-无符号不匹配的警告-sizeof使用注意事项]
  • [CISCN2019 华东南赛区]Web11
  • [DL]深度学习_Feature Pyramid Network
  • [error] 17755#0: *58522 readv() failed (104: Connection reset by peer) while reading upstream
  • [HTML]Web前端开发技术12(HTML5、CSS3、JavaScript )——喵喵画网页
  • [js高手之路] dom常用API【appendChild,insertBefore,removeChild,replaceChild,cloneNode】详解与应用...
  • [LeetCode]284. Peeking Iterator(C++,类,暴力)