PAT甲级刷题记录——1080 Graduate Admission (30分)

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 G​E​​ , and the interview grade G​I​​ . The final grade of an applicant is (G​E​​ +G​I​​ )/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 G​E​​ . 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.

Input Specification:

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 G​E​​ and G​I​​ , 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.

Output Specification:

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.

Sample Input:

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

Sample Output:

0 10
3
5 6 7
2 8

1 4

思路

这题的题干比较长哈,我这里作个简要的翻译:

现在给出一些学生的初试成绩GE和面试成绩GI,以及他们填写的志愿学校,让你来决定哪些学校应该录取哪些学生。

首先,输入的第一行是N、M、K,其中,N是N个学生的简历,包含了他的GE和GI,以及想去的学校(K个),M是学校的个数,K是志愿的个数(也就是学生能填K个志愿,优先当然是第一志愿啦~);

然后,第二行是M个学校的招生名额,这里要注意的是,学校的编号是从0~M-1的;

最后,总共包括N行,每行代表着一个学生的简历(学生的简历编号从0~N-1计算),这里为了存放的简单,我把每个学生想去的学校都用了vector容器来存放。

输出的话,就是对应0~M-1个学校,每个学校占据一行,每一行升序输出学生的简历编号。

然后就是排序的要求:

  • 首先按照总成绩排序(即:(GE+GI)/2),总成绩高的自然排在前面,所以是降序排列;
  • 如果两个学生的总成绩相等,那么初始成绩GE分数高的排在前面;
  • 如果总成绩和GE都相等,那么算作同一个rank(这里要重点注意一下,因为后面要用到)。

为了方便处理每个学生的rank排名,我这里把rank也写进了结构体里面,然后对List(存放所有学生信息的vector容器)排完序之后,再遍历一遍List,以求得每个学生的rank排名。

求rank的方法还是一样的:如果总成绩和GE都和前一个学生相等的话,就把前一个学生的rank直接给当前学生,如果不是,那么当前学生的rank就是i+1(因为vector容器是从0开始的,但是排名是从1开始的,所以要记得+1)。

然后就是按排名和志愿来录取学生啦~

再遍历一遍List(存放学生信息的容器),寻找当前List[i]的所有志愿(即:int v = List[i].preferredSchools[j],v是当前学生i的某个心仪学校,为了方便之后的应用,我用了一个新变量v来代替),如果当前学校还有招生名额或者当前学生的rank和上个学生的rank一样且当前学生的当前心仪学校v和上个学生被录取的学校相同,那么这个学校v就要录取这个学生,于是,s[v].push_back(List[i].appNumber)(为了方便后续的输出,我这里也用了一个vector来存放学校的录取情况,即:vector<int> s[101],这样的话,下标就是对应的学校编号,其内容就是录取的学生简历编号)。

最后,因为输出要按简历编号升序输出,因此,在遍历每一个学校的录取情况时,先做个排序就好啦~

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
struct student{
    int Rank;
    int appNumber;
    int GE, GI;
    double final_Grade;
    vector<int> preferredSchools;
};
vector<student> List;
vector<int> s[101];
int school[101] = {0};
bool cmp(student a, student b){
    if(a.final_Grade!=b.final_Grade) return a.final_Grade>b.final_Grade;
    else return a.GE>b.GE;
}
int main()
{
    int N, M, K;
    cin>>N>>M>>K;
    for(int i=0;i<M;i++){
        cin>>school[i];
    }
    for(int i=0;i<N;i++){
        student tmp;
        int tmpGE, tmpGI;
        cin>>tmpGE>>tmpGI;
        tmp.GE = tmpGE;
        tmp.GI = tmpGI;
        tmp.final_Grade = (tmpGE+tmpGI)/2.0;
        tmp.appNumber = i;
        for(int j=0;j<K;j++){
            int tmpschool;
            cin>>tmpschool;
            tmp.preferredSchools.push_back(tmpschool);
        }
        List.push_back(tmp);
    }
    sort(List.begin(), List.end(), cmp);
    int LastRank = 0, LastGrade = 0, LastGE = 0;
    for(int i=0;i<List.size();i++){
        if(i==0){
            List[i].Rank = i+1;
            LastRank = List[i].Rank;
            LastGrade = List[i].final_Grade;
            LastGE = List[i].GE;
        }
        else{
            if(List[i].final_Grade==List[i-1].final_Grade&&List[i].GE==List[i-1].GE){
                List[i].Rank = LastRank;
            }
            else{
                List[i].Rank = i+1;
                LastRank = List[i].Rank;
                LastGrade = List[i].final_Grade;
                LastGE = List[i].GE;
            }
        }
    }
    int LastSchool = 0;
    for(int i=0;i<List.size();i++){
        for(int j=0;j<List[i].preferredSchools.size();j++){
            int v = List[i].preferredSchools[j];
            if(school[v]>0||i>0&&List[i].Rank==List[i-1].Rank&&v==LastSchool){
                s[v].push_back(List[i].appNumber);
                school[v]--;
                LastSchool = v;
                break;
            }
        }
    }
    for(int i=0;i<M;i++){
        sort(s[i].begin(), s[i].end());
        for(int j=0;j<s[i].size();j++){
            if(j==0) cout<<s[i][j];
            else cout<<" "<<s[i][j];
        }
        cout<<'\n';
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值