【算法笔记题解】PAT A.1080 Graduate Admission (30 分)

全文目录

前言

题目描述

输入描述

输出描述

输入示例

输出示例

解题思路

题目理解

相关思路

代码实现

结果分析与总结


前言

昨天刷的那个被牛客网坑了一把,今天PAT恢复了,喜大普奔啊。这是今天第二道题。 相关源码我都更新在gitee上了需要自取xingleigao/study - Gitee.com


题目描述

1080 Graduate Admission (30 分)https://pintia.cn/problem-sets/994805342720868352/problems/994805387268571136https://pintia.cn/problem-sets/994805342720868352/problems/994805387268571136

It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admission procedure.

Each applicant will have to provide two grades: the national entrance exam grade GE​, and the interview grade GI​. The final grade of an applicant is (GE​+GI​)/2. The admission rules are:

  • The applicants are ranked according to their final grades, and will be admitted one by one from the top of the rank list.

  • If there is a tied final grade, the applicants will be ranked according to their national entrance exam grade GE​. If still tied, their ranks must be the same.

  • Each applicant may have K choices and the admission will be done according to his/her choices: if according to the rank list, it is one's turn to be admitted; and if the quota of one's most preferred shcool is not exceeded, then one will be admitted to this school, or one's other choices will be considered one by one in order. If one gets rejected by all of preferred schools, then this unfortunate applicant will be rejected.

  • If there is a tied rank, and if the corresponding applicants are applying to the same school, then that school must admit all the applicants with the same rank, even if its quota will be exceeded.

输入描述

Each input file contains one test case.

Each case starts with a line containing three positive integers: N (≤40,000), the total number of applicants; M (≤100), the total number of graduate schools; and K (≤5), the number of choices an applicant may have.

In the next line, separated by a space, there are M positive integers. The i-th integer is the quota of the i-th graduate school respectively.

Then N lines follow, each contains 2+K integers separated by a space. The first 2 integers are the applicant's GE​ and GI​, respectively. The next K integers represent the preferred schools. For the sake of simplicity, we assume that the schools are numbered from 0 to M−1, and the applicants are numbered from 0 to N−1.

输出描述

For each test case you should output the admission results for all the graduate schools. The results of each school must occupy a line, which contains the applicants' numbers that school admits. The numbers must be in increasing order and be separated by a space. There must be no extra space at the end of each line. If no applicant is admitted by a school, you must output an empty line correspondingly.

输入示例

11 6 3
2 1 2 2 2 3
100 100 0 1 2
60 60 2 3 5
100 90 0 3 4
90 100 1 2 0
90 90 5 1 3
80 90 1 0 2
80 80 0 1 2
80 80 0 1 2
80 70 1 3 2
70 80 1 2 3
100 100 0 2 4

输出示例

0 10
3
5 6 7
2 8

1 4

解题思路

题目理解

PAT A级别的所有题目都是英文,其实题主作为一个六级都没考过的人都能看懂,相信大家都能看懂吧,看不懂也没关系,经常出现的单词也不多,多看就好了.

其实就是高考选志愿,一共有n个学生,m个学校,每个学生可以选择k个志愿。然后录取规则就是按照分数排序依次看志愿学校还有没有空位,同时如果学校没空位,但是和最后一名的成绩相同那么就会破格录取。

排名规则是按总分/2(这里除2不除2没啥区别),然后如果总分相同按照GE排名,如果GE也相同两人就是同名次。

如果学生达不到每个志愿学校的要求则落榜。

相关思路

分为多个步骤

1.读入相应数据,并对学生进行排序得到学生的排名。(这里因为输出为输入的顺序,所以要把输入的学生id保存下来)

2.根据每个学生的志愿学校判断是否符合学校的录取要求,符合则录取。

3.对每个学校录取的人id进行降序排序(需要调用id所以,学生结构体数组要放在堆区),然后根据要求进行输出。

代码实现

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct student{
    int GE, GI, sum;//学生成绩
    int rank, id;      //学生id和排名
    int select[6];      //选择的志愿
}stu[40010];

struct School{
    int quota;      //招生额度
    int stuNum;     //当前招生人数
    int id[40010];  //招收的考生编号
    int lastAdmit;  //最后一个招收的编号
}sch[110];

bool cmp(student a, student b){//根据要求排序
    if(a.sum != b.sum)  return a.sum > b.sum;
    else return a.GE > b.GE;
}
bool cmpID(int a, int b){
    return stu[a].id < stu[b].id;
}

int main(){
    int n, k, m;
    scanf("%d%d%d", &n, &m, &k);

    for(int i = 0; i < m; ++i){
        scanf("%d", &sch[i].quota);//读入学校招生额度
        sch[i].lastAdmit = -1;
        sch[i].stuNum = 0;
    }

    for(int i = 0; i < n; ++i){                         //读入考生数据
        scanf("%d%d", &stu[i].GE, &stu[i].GI);
        stu[i].sum = stu[i].GE + stu[i].GI;
        stu[i].id = i;
        for(int j = 0; j < k; ++j)  scanf("%d",&stu[i].select[j]);
    }
    
    sort(stu, stu + n, cmp);    //排序确定名词
    for(int i = 0; i < n; ++i){
        if(i > 0&& stu[i].sum == stu[i - 1].sum && stu[i].GE == stu[i - 1].GE)  stu[i].rank = stu[i - 1].rank;
        else stu[i].rank = i;
    }

    for(int i = 0; i < n; ++i){ //对每个学生进行判定录取
        for(int j = 0; j < k; j++){
            int choice = stu[i].select[j];
            int num = sch[choice].stuNum;
            int last = sch[choice].lastAdmit;
            if(num < sch[choice].quota || (last != -1 && stu[i].rank == stu[last].rank)){//满足条件则录取
                sch[choice].id[num] = i;
                sch[choice].stuNum ++;
                sch[choice].lastAdmit = i;
                break;
            }
        }
    }

    for(int i = 0; i < m; i++){
        if(sch[i].stuNum){
            sort(sch[i].id, sch[i].id + sch[i].stuNum, cmpID);
            printf("%d",stu[sch[i].id[0]].id);
            for(int j = 1; j < sch[i].stuNum; j++)  printf(" %d",stu[sch[i].id[j]].id);
        }
        printf("\n");
    }

    return 0;   
}

结果分析与总结

PAT官网所有测试点都通过了。

创建结构体之后直接定义的结构体数组是申请在堆区的可以让所有子函数调用,而程序内定义的结构体数组保存在栈区,调用其它函数的时候无法调用。小坑

 现在觉得linux的老师还是很厉害的,提到的堆栈在平时运用的时候非常常用,可以看懂程序出错的原因和解决方案,得好好学啊。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XingleiGao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值