【九度OJ】1005:Graduate Admission

地址:
http://ac.jobdu.com/problem.php?pid=1005
题目描述:
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 may contain more than 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
来源:
2011年浙江大学计算机及软件工程研究生机试真题

源码:

#include<stdio.h>
#include<algorithm>

using namespace std;

#define MAX_N 40005
#define MAX_M 105
#define MAX_K 10

int n, m, k;
struct Student{
    int studentID;    //学生的ID
    float ge;         //学生的成绩GE
    float gi;         //学生的成绩GI
    int school[ MAX_K ];     //该学生的目标院校,优先级随着下标的递增而递增
    int rank;         //学生排名
    int admittedSchool;   //录取改学生的学校,初始值为-1
}stu[ MAX_N ];

int graSchool[ MAX_M ][ 2 ];    //第一列记录学校的招生名额,第二列记录该校当前招取的最后一人的排名,第二列初始化-1

//排序规则:按照GE和GI的平均分排序,若平均分相同,按照GE排序,若任然相同,则排名相同
bool cmp( Student stu1, Student stu2 ){    
    float medScore1 = ( stu1.ge + stu1.gi ) / 2;
    float medScore2 = ( stu2.ge + stu2.gi ) / 2;

    if( medScore1 != medScore2 ){
        return medScore1 > medScore2;
    }
    else {
        return stu1.ge > stu1.ge;
    }
}

//排序规则:按照录取学校序号排序,若序号相同,按照学生序号排序,均是从小到大
bool cmpPrint( Student stu1, Student stu2 ){
    if( stu1.admittedSchool != stu2.admittedSchool ){
        return stu1.admittedSchool < stu2.admittedSchool;
    }
    else {
        return stu1.studentID < stu2.studentID;
    }
}

void getRank(){
    float medScore1;
    float medScore2;

    int ran = 1;
    stu[ 0 ].rank = ran;
    for( int i = 1; i < n; i ++ ){
        medScore1 = ( stu[ i - 1 ].ge + stu[ i - 1 ].gi ) / 2;
        medScore2 = ( stu[ i ].ge + stu[ i ].gi ) / 2;

        //该同学和上一个同学的排名不相同
        if( medScore1 != medScore2 || stu[ i - 1 ].ge != stu[ i ].ge ){
            ran++;
        }

        stu[ i ].rank = ran;
    }
}

//按顺序里录取学生
void enroll(){
    for( int i = 0; i < n; i ++ ){
        for( int j = 0; j < k; j ++ ){
            if( graSchool[ stu[i].school[j] ][ 0 ] > 0 ){  //该学校还有名额
                stu[i].admittedSchool = stu[i].school[j];   //该学生的录取院校
                graSchool[ stu[i].school[j] ][ 0 ]--;  //学院名额-1
                graSchool[ stu[i].school[j] ][ 1 ] = stu[ i ].rank;   //该院校最后录取的一个人的排名
                break;
            }
            else if( graSchool[ stu[i].school[j] ][ 0 ] == 0 ){   //如果这个学校没有名额了,但是这个学校要招收排名相同的学生
                if( graSchool[ stu[i].school[j] ][ 1 ] == stu[ i ].rank ){
                    stu[i].admittedSchool = stu[i].school[j];   //该学生的录取院校
                    break;
                }
            }
        }
    }
}

//按学校输出录取学生,这个时候,学生都已经按照录取学校排好序
void printStu(){
    int schoolId = 0;
    int count = 0;
    int stuId = 0;
    while( schoolId < m ){
        if( stu[ stuId ].admittedSchool == -1 ){
            stuId ++;
            continue;
        }

        if( stu[ stuId ].admittedSchool == schoolId ){
            count ++;

            if( count == 1 ){
                printf("%d", stu[ stuId ].studentID );
            }
            else{
                printf(" %d", stu[ stuId ].studentID );
            }
        }
        else{
            stuId --;
            schoolId ++;   //院系编号+1
            count = 0;
            printf("\n");
        }
        stuId ++;
    }   
}
int main(){
    while( scanf( "%d %d %d", &n, &m, &k ) != EOF ){
        //输入各个学校的招生指标
        for( int i = 0; i < m; i ++ ){
            scanf( "%d", &graSchool[ i ][ 0 ] );
            graSchool[ i ][ 1 ] = -1;
        }
        //输入学生的成绩和目标院校
        for( int i = 0; i < n; i ++ ){
            stu[ i ].studentID = i;   //学生ID
            stu[ i ].admittedSchool = -1;   //录取该学生的学校

            scanf( "%f %f", &stu[ i ].ge, &stu[ i ].gi );
            //学生的目标院校
            for( int j = 0; j < k; j ++ ){
                scanf( "%d", &stu[ i ].school[ j ] );
            }
        }

        sort( stu, stu+n, cmp);
        getRank();   //获取学生的排名
        enroll();    //按顺序录取学生
        sort( stu, stu+n, cmpPrint);

        printStu();
    }
}
/**************************************************************
    Problem: 1005
    User: 螺小旋
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:3372 kb
****************************************************************/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章