2022年天梯赛L1-8真题解析(哈希+排序)

L1-088 静静的推荐(哈希表+排序)

题目链接:

https://pintia.cn/problem-sets/994805046380707840/exam/problems/type/7?problemSetProblemId=1518582000729911296

解题思路:主要是通过哈希表+排序来解决

易错点:
这里题目说"如果有的学生天梯赛成绩虽然与前一个人相同,但其参加过 PAT 考试,且成绩达到了该企业的面试分数线,则也可以接受。"
指的应该是只要达到了就都接受,不用管批次,所以在处理这类条件的人的时候用while循环


解题代码如下:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
int main() {
    int N,K,S;
    cin>>N>>K>>S;
    int score,pat;
    unordered_map<int,int> student;//key是分数
    unordered_set<int> scores;
    unordered_map<int,int> extra_num;
    for(int i=0;i<N;i++){
        cin>>score>>pat;
        if(score<175){
            continue;//不考虑
        }
        scores.insert(score);
        if(pat>=S){
            extra_num[score]++;
        }
        else{
            student[score]++;
        }
    }
    int sum=0;
    vector<int> a_scores(scores.begin(),scores.end());
    sort(a_scores.begin(),a_scores.end());
    //哈希表要用pair结构排序
    //本题就是分两类,一类是普通学生,一类是特殊学生
    for(int h=0;h<K;h++){
        for(int i=0;i<a_scores.size();i++){
            int score_num=a_scores[i];
            if(student[score_num]>0){
                student[score_num]--;
                sum++;
            }
            //他的意思是如果有很多学生也达到了这个要求的话,也可以都报上去
            while(extra_num[score_num]>0){
                extra_num[score_num]--;
                sum++;
            }
        }
    }
    cout<<sum<<endl;
    return 0;
}
相关推荐
aaaameliaaa24 分钟前
进制练习题【找出只出现一次的数字、交换两个变量(不创建临时变量)、统计二进制中1的个数、打印整数二进制的奇数位和偶数位、求两个数二进制中不同位的个数】
c语言·数据结构·笔记·算法
QiLinkOS2 小时前
第三视觉理解徐玉生与他的商业活动(28)
大数据·c++·人工智能·算法·开源协议
wabs6663 小时前
关于动态规划【力扣1143.最长公共子序列的思考】
算法·leetcode·动态规划
剑挑星河月3 小时前
54.螺旋矩阵
java·算法·leetcode·矩阵
Robot_Nav3 小时前
MPPI 局部规划器实验设计讲解
人工智能·算法·mppi
mingo_敏4 小时前
Mean-Teacher 均值教师自训练框架详解
算法·均值算法
星空露珠4 小时前
迷你世界UGc3.0脚本Wiki[剧情动画模块管理接口 Timeline]
开发语言·数据结构·算法·游戏·lua
笨笨没好名字4 小时前
Leetcode刷题python3版第一周(下)
linux·算法·leetcode
jinyishu_4 小时前
常见排序算法详解
数据结构·算法·排序算法
手写码匠4 小时前
手写 LLM 安全护栏:从内容审核到越狱防御的完整实现
人工智能·深度学习·算法·aigc