【九度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
****************************************************************/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章