PAT-A1080 Graduate Admission 题目内容及题解

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

题目大意

题目要求编程协助浙江省研究生院的申请信息。

  • 所有申请者根据总成绩进行排名,并将从排名首位的学校逐一录取。
  • 如果最终成绩并列,申请人将根据他们的入学考试成绩排名。如果仍然势均力敌,他们的排名一致。
  • 每位申请人可有K项选择,并会按其选择进行尝试录取,当所有选择的学校均被录取满,该申请人将不幸落榜。
  • 如果有并列的排名的申请人申请同一所学校并达到录取条件,那么即使超出排名,该学校必须录取所有排名相同的申请人。

题目要求输出所有研究生院的录取结果。

解题思路

  1. 分别建立学生和学校的结构体用于存储数据;
  2. 初始化结构体并输入数据;
  3. 对学生进行排名并记录总排名;
  4. 模拟申请过程,按照排名依次投档,同时更新学校的剩余名额、录取门槛(最后一名的排名情况以应对录取名额已满但是排名一致的情况)和录取列表;
  5. 按照题目要求输出结果并返回零值。

代码

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

#define maxn 40010
#define INF 10000000 

struct Student{
    int Ge,Gi,sum;
    int apply[5];
    int rank;
    int id;
}stu[maxn];

struct School{
    int quota;
    int rank;
    vector<int> admit;
}sch[110];

int N,M,K; 

bool cmp(Student a,Student b){
    if(a.sum!=b.sum){
        return a.sum>b.sum;
    }else if(a.Ge!=b.Ge){
        return a.Ge>b.Ge;
    }else{
        return a.id<b.id;
    }
}

void Init(){
    int i,j;
    scanf("%d%d%d",&N,&M,&K);
    for(i=0;i<M;i++){
        scanf("%d",&sch[i].quota);
        sch[i].rank=INF; 
    }
    for(i=0;i<N;i++){
        stu[i].id=i;
        scanf("%d%d",&stu[i].Ge,&stu[i].Gi);
        stu[i].sum=stu[i].Ge+stu[i].Gi;
        for(j=0;j<K;j++){
            scanf("%d",&stu[i].apply[j]);
        }
    }
    sort(stu,stu+N,cmp);
    stu[0].rank=0;
    for(i=1;i<N;i++){
        if((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;
        }
    }
}

void Apply(){
    int i,j;
    for(i=0;i<N;i++){
        for(j=0;j<K;j++){
            if(sch[stu[i].apply[j]].quota>0||sch[stu[i].apply[j]].rank==stu[i].rank){
                sch[stu[i].apply[j]].quota--;
                sch[stu[i].apply[j]].rank=stu[i].rank;
                sch[stu[i].apply[j]].admit.push_back(stu[i].id);
                break; 
            }
        }
    }
}

void Print(){
    int i,j;
    for(i=0;i<M;i++){
        sort(sch[i].admit.begin(),sch[i].admit.end());
        for(j=0;j<sch[i].admit.size();j++){
            printf("%d",sch[i].admit[j]);
            if(j<sch[i].admit.size()-1){
                printf(" ");
            }
        }
        printf("\n");
    }
}

int main(){
    Init();
    Apply();
    Print();
    return 0;
}

运行结果

PAT-A1080 Graduate Admission 题目内容及题解