PAT A1080 Graduate Admission (30)

解题思路:

基本思路是依照题目给定的规则排序,然后遍历结构体,将对应的申请送到对应学校的队列中(这里用了优先队列,是直接排好序的)。
学校也可以构造结构体来储存申请,遍历完申请可以直接再排一下序。

AC代码

// grade1 grade2 final = (grade1 + grade2) / 2
// ranked by final
// if a tie ranked by grade1, if still a tie, they have same rank
// k choices
// first choice full, try second, try third, or rejected
// *** if some has same rank and same choices, school must accept all even if will be exceeded
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <functional>
using namespace std;

const int maxn = 40010, maxm = 110, maxk = 5;
//int applicants[maxn]{ 0 };
struct applicant {
    int id, g1, g2, f, r, choices[maxk];
} app[maxn];

bool cmp(applicant a, applicant b) {
    if (a.f != b.f) return a.f > b.f;
    else if (a.g1 != b.g1) return a.g1 > b.g1;
    else return a.g2 > b.g2;                            // 表示rank相同
}
void init() {
    for (int i = 0; i < maxn; i++) {
        app[i].r = 0;
        memset(app[i].choices, -1, sizeof(app[i].choices));
    }
}

int school[maxm]{ 0 };
priority_queue<int, vector<int>, greater<int>> received[maxm];
int main() {
    int n, m, k, i, j;
    scanf("%d%d%d", &n, &m, &k);

    for (i = 0; i < m; i++)
        scanf("%d", &school[i]);
    init();
    for (i = 0; i < n; i++) {
        app[i].id = i;
        scanf("%d%d", &app[i].g1, &app[i].g2);
        app[i].f = app[i].g1 + app[i].g2;
        for (j = 0; j < k; j++)
            scanf("%d", &app[i].choices[j]);
    }

    sort(app, app + n, cmp);
    // ranking
    app[0].r = 1;
    for ( i = 1; i < n; i++)
    {
        if (app[i].f == app[i - 1].f) {
            if (app[i].g1 == app[i - 1].g1)
                app[i].r = app[i - 1].r;
            else
                app[i].r = i + 1;
        }
        else
            app[i].r = i + 1;
    }
    int lastrank = 0, lastreceived = -1;
    for ( i = 0; i < n; i++)
    {
        int a;
        for ( j = 0; j < k; j++)
        {
            a = app[i].choices[j];
            if (school[a] > 0) {
                school[a]--;
                received[a].push(app[i].id);
                break;
            }
            else if (school[a] == 0 && lastrank == app[i].r && lastreceived == j)
            {
                received[a].push(app[i].id);
                break;
            }
        }
        lastrank = app[i].r;
        lastreceived = j;
    }

    for ( i = 0; i < m; i++)
    {
        j = received[i].size();

        while (!received[i].empty())
        {
            printf("%d", received[i].top());
            received[i].pop();
            if (!received[i].empty())
                printf(" ");
        }
        printf("\n");
    }

    return 0;
}

pap

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值