1080 Graduate Admission (30 分)

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

思路

  • 建立一個(gè)結(jié)構(gòu)體用于存儲(chǔ)學(xué)生的信息,建立一個(gè)結(jié)構(gòu)體數(shù)組存儲(chǔ)所有的學(xué)生癞埠。
struct student
{
    int ge;
    int gi;
    int choices[maxk]; // 記錄學(xué)生的志愿效斑。
    int key; // 記錄學(xué)生的id珍语,因?yàn)橹髸?huì)進(jìn)行排序顽铸,學(xué)生的索引便不再是他們的id了端盆。
} Stu[maxn];
  • 建立一個(gè)vector<int> schAdm[maxm]; 記錄每個(gè)學(xué)校錄取的學(xué)生合蔽。
  • 設(shè)計(jì)一個(gè)排序函數(shù)击敌,將學(xué)生按分?jǐn)?shù)從高到低排序。
bool cmp(student s1, student s2) {
    if((s1.ge + s1.gi) != (s2.ge + s2.gi)) {
        return (s1.ge + s1.gi) > (s2.ge + s2.gi);
    }
    else
        return s1.ge > s2.ge;
}
  • 排序后拴事,遍歷結(jié)構(gòu)體數(shù)組谅猾,將學(xué)生添加到對(duì)應(yīng)學(xué)校中(如果沒(méi)有滿額)沼溜,如果滿額了,則與該學(xué)校錄取的最后一名學(xué)生的成績(jī)進(jìn)行比較,如果成績(jī)完全一樣凳忙,則超額錄取。否則军洼,遍歷該學(xué)生之后的志愿锤窑。

注意點(diǎn)

  • 如何設(shè)計(jì)滿額后也可以判斷是否繼續(xù)錄取學(xué)生的功能。
// 當(dāng)學(xué)校滿員后調(diào)用該函數(shù),判斷時(shí)候可以超額錄取十厢。
bool isExceeded(int stuid, int schid) {
    int last_stu_id = schAdm[schid][schQuota[schid]-1]; // 獲取學(xué)校的學(xué)生中最后一名的索引
    if((Stu[last_stu_id].ge == Stu[stuid].ge) && (Stu[last_stu_id].gi ==  Stu[stuid].gi))
        return true;
    else return false;

}

AC代碼

#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 40001;
const int maxm = 101;
const int maxk = 5;

struct student
{
    int ge;
    int gi;
    int choices[maxk];
    int key;
} Stu[maxn];

// 排序函數(shù)
bool cmp(student s1, student s2) {
    if((s1.ge + s1.gi) != (s2.ge + s2.gi)) {
        return (s1.ge + s1.gi) > (s2.ge + s2.gi);
    }
    else
        return s1.ge > s2.ge;
}

int schQuota[maxm]; // 每個(gè)學(xué)校接受學(xué)生的名額上限
vector<int> schAdm[maxm]; // 記錄每個(gè)學(xué)校錄取的學(xué)生
// 判斷是否可以超限添加(當(dāng)兩者的分?jǐn)?shù)完全一樣等太,學(xué)校必須接收,無(wú)論名額是否超限)
bool isExceeded(int stuid, int schid) {
    int last_stu_id = schAdm[schid][schQuota[schid]-1];
    if((Stu[last_stu_id].ge == Stu[stuid].ge) && (Stu[last_stu_id].gi ==  Stu[stuid].gi))
        return true;
    else return false;

}

int main() {
    int n, m, k;
    scanf("%d%d%d", &n, &m, &k);

    for (int i = 0; i < m; ++i)
    {
        scanf("%d", &schQuota[i]);
    }

    for (int i = 0; i < n; ++i)
    {
        int ge, gi;
        scanf("%d%d", &ge, &gi);
        Stu[i].ge = ge;
        Stu[i].gi = gi;
        Stu[i].key = i;
        for(int j = 0; j < k; j++) {
            int choice;
            scanf("%d", &choice);
            Stu[i].choices[j] = choice;
        }
    }

    sort(Stu, Stu+n, cmp);


    for (int i = 0; i < n; ++i)
    {
        int* choices;
        choices = Stu[i].choices;
        for (int j = 0; j < k; ++j)
        {
            //printf("schAdm[%d]: %d %d\n", choices[j], schAdm[choices[j]].size(), schQuota[choices[j]]);
            if(schAdm[choices[j]].size() < schQuota[choices[j]]) {
                //int stuid = Stu[i].key;
                schAdm[choices[j]].push_back(i); // 記錄的是排序后學(xué)生所在數(shù)組的序號(hào)蛮放,并不是學(xué)生真正的id
                break;
            }
            else {
                if(isExceeded(i, choices[j])) {
                    schAdm[choices[j]].push_back(i);
                    break;
                }

            }
        }
    }

    for (int i = 0; i < m; ++i)
    {
        vector<int> adm = schAdm[i]; // 因?yàn)橛涗浀氖桥判蚝笤跀?shù)組中的序號(hào)缩抡,這里獲取原來(lái)的id
        vector<int> ids;
        for (int j = 0; j < adm.size(); ++j)
        {
            int id = Stu[adm[j]].key;
            ids.push_back(id);
        }
        sort(ids.begin(), ids.end());
        for (int j = 0; j < ids.size(); ++j)
        {
            printf("%d", ids[j]);
            if(j != ids.size()-1) printf(" ");
        }
        printf("\n");
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市包颁,隨后出現(xiàn)的幾起案子瞻想,更是在濱河造成了極大的恐慌,老刑警劉巖徘六,帶你破解...
    沈念sama閱讀 206,311評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件内边,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡待锈,警方通過(guò)查閱死者的電腦和手機(jī)漠其,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)竿音,“玉大人和屎,你說(shuō)我怎么就攤上這事〈核玻” “怎么了柴信?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,671評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)宽气。 經(jīng)常有香客問(wèn)我随常,道長(zhǎng),這世上最難降的妖魔是什么萄涯? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,252評(píng)論 1 279
  • 正文 為了忘掉前任绪氛,我火速辦了婚禮,結(jié)果婚禮上涝影,老公的妹妹穿的比我還像新娘枣察。我一直安慰自己,他們只是感情好燃逻,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,253評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布序目。 她就那樣靜靜地躺著,像睡著了一般伯襟。 火紅的嫁衣襯著肌膚如雪猿涨。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,031評(píng)論 1 285
  • 那天姆怪,我揣著相機(jī)與錄音嘿辟,去河邊找鬼舆瘪。 笑死,一個(gè)胖子當(dāng)著我的面吹牛红伦,可吹牛的內(nèi)容都是我干的英古。 我是一名探鬼主播,決...
    沈念sama閱讀 38,340評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼昙读,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼召调!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起蛮浑,我...
    開(kāi)封第一講書(shū)人閱讀 36,973評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤唠叛,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后沮稚,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體艺沼,經(jīng)...
    沈念sama閱讀 43,466評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,937評(píng)論 2 323
  • 正文 我和宋清朗相戀三年蕴掏,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了障般。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,039評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡盛杰,死狀恐怖挽荡,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情即供,我是刑警寧澤定拟,帶...
    沈念sama閱讀 33,701評(píng)論 4 323
  • 正文 年R本政府宣布,位于F島的核電站逗嫡,受9級(jí)特大地震影響青自,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜驱证,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,254評(píng)論 3 307
  • 文/蒙蒙 一延窜、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧雷滚,春花似錦需曾、人聲如沸吗坚。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,259評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)商源。三九已至车份,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間牡彻,已是汗流浹背扫沼。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工出爹, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人缎除。 一個(gè)月前我還...
    沈念sama閱讀 45,497評(píng)論 2 354
  • 正文 我出身青樓严就,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親器罐。 傳聞我的和親對(duì)象是個(gè)殘疾皇子梢为,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,786評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容