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

思路

  • 建立一个结构体用于存储学生的信息,建立一个结构体数组存储所有的学生。
struct student
{
    int ge;
    int gi;
    int choices[maxk]; // 记录学生的志愿。
    int key; // 记录学生的id,因为之后会进行排序,学生的索引便不再是他们的id了。
} Stu[maxn];
  • 建立一个vector<int> schAdm[maxm]; 记录每个学校录取的学生。
  • 设计一个排序函数,将学生按分数从高到低排序。
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;
}
  • 排序后,遍历结构体数组,将学生添加到对应学校中(如果没有满额),如果满额了,则与该学校录取的最后一名学生的成绩进行比较,如果成绩完全一样,则超额录取。否则,遍历该学生之后的志愿。

注意点

  • 如何设计满额后也可以判断是否继续录取学生的功能。
// 当学校满员后调用该函数,判断时候可以超额录取。
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;

}

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];

// 排序函数
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]; // 每个学校接受学生的名额上限
vector<int> schAdm[maxm]; // 记录每个学校录取的学生
// 判断是否可以超限添加(当两者的分数完全一样,学校必须接收,无论名额是否超限)
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); // 记录的是排序后学生所在数组的序号,并不是学生真正的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]; // 因为记录的是排序后在数组中的序号,这里获取原来的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");
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,014评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,796评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,484评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,830评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,946评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,114评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,182评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,927评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,369评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,678评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,832评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,533评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,166评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,885评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,128评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,659评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,738评论 2 351

推荐阅读更多精彩内容