1. 程式人生 > 其它 >1080 Graduate Admission (30 分)(排序)【回顧】

1080 Graduate Admission (30 分)(排序)【回顧】

It is said that in 2013, there were 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.

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 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.

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

生詞

英文 解釋
Graduate Admission 研究生入學
automate 使自動化
quota 限額

分析:

1.設立stu結構體,儲存學生的id(防止排序後id打亂了順序),GE和GI的成績,總評成績,排名,志願學校的列表陣列。
2.設立sch結構體,儲存school[i]招生的名額限制maxNum,現在已經招收了的學生個數nowNum,招收的學生的id列表stuID,以及當前已經招收了的學生的排名的最後一名lastRank。
3.把學生按照成績進行排序,並賦值排名。如果GE一樣且Grade一樣,他們的名次就是一樣的。
4.從第一個學生開始,根據他的志願,來嘗試被學校錄取。如果當前學校名額未滿。那麼就錄取進去,並且讓學校的nowNum加1.並且更新lastRank為這個學生的rank。如果當前學校的lastRank等於自己的rank,那麼不管名額滿不滿都錄取。而且記得把學生的id新增到學校的stuID列表中。

5.輸出的時候因為id順序是亂的,要先從小到大排序,然後輸出。每個學校佔一行、

原文連結:https://blog.csdn.net/liuchuo/article/details/52493707

題解

這裡用的存放 學校錄取的學生列表 結構些許複雜,拆開看一下:

sch[0]					sch[1]
	0:stu.id=0				0:stu.id=3
	1:stu.id=10	

sch[2]					sch[4]
	0:stu.id=5				null
	1:stu.id=6
	2:stu.id=7

上面是根據學生id排序後的sch陣列,他的每一個元素是一個vector<peo>,下面舉例說明一下如果兩個學生的fin與ge成績都相同是如何比較的:

stu[i].fin==sch[schid][lastindex].fin && stu[i].ge==sch[schid][lastindex].ge

1.先定位到sch[schid],然後它是一個vector<peo>向量嘛;

2.接下來定位到他的最後一名學生那個結構體,比如sch[0][1],他就是10號學生;

3.之後找到他的fin成績,與當前學生stu[i].fin進行比較;

4.同理,ge成績也是這樣找的。

#include <bits/stdc++.h>

using namespace std;
struct peo
{
    int id,ge,gi,fin;
    vector<int> choice;
};
bool cmp(peo a,peo b){
    if(a.fin!=b.fin) return a.fin>b.fin;
    return a.ge>b.ge;
}
bool cmp1(peo a,peo b){
    return a.id<b.id;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int n,m,k,quota[110],cnt[110]={0};
    cin>>n>>m>>k;
    vector<peo> stu(n),sch[110];
    for(int i=0;i<m;i++){
        cin>>quota[i];
    }
    for(int i=0;i<n;i++){
        cin>>stu[i].ge>>stu[i].gi;
        stu[i].id=i;
        stu[i].fin=stu[i].ge+stu[i].gi;
        stu[i].choice.resize(k);
        for(int j=0;j<k;j++){
            cin>>stu[i].choice[j];
        }
    }
    sort(stu.begin(),stu.end(),cmp);
    for(int i=0;i<n;i++){
        for(int j=0;j<k;j++){
            int schid=stu[i].choice[j];
            int lastindex=cnt[schid]-1;
            if(cnt[schid]<quota[schid]||stu[i].fin==sch[schid][lastindex].fin && stu[i].ge==sch[schid][lastindex].ge){
                sch[schid].push_back(stu[i]);
                cnt[schid]++;
                break;
            }
        }
    }
    for(int i=0;i<m;i++){
        sort(sch[i].begin(),sch[i].end(),cmp1);
        for(int j=0;j<cnt[i];j++){
            if(j!=0) cout<<" ";
            cout<<sch[i][j].id;
        }
        cout<<endl;
    }
    return 0;
}

本文來自部落格園,作者:勇往直前的力量,轉載請註明原文連結:https://www.cnblogs.com/moonlight1999/p/15718979.html