PAT 甲级 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 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 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.

输出

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.

思路

排序,看学校名额是否满了,注意一个排名的人都要被录进去

代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
int sch[105];


typedef struct node {
    
    
	int GE;
	int All;
	int ch[6];
	int number;
}Grade;
Grade grade[40005];
Grade temp[40005];
vector<int> sc[105];
int cmp(const void* a, const void* b)
{
    
    
	Grade* c = (Grade*)a;
	Grade* d = (Grade*)b;
	if (c->All != d->All) return d->All - c->All;
	else return d->GE - c->GE;
}
int main()
{
    
    
	int N, M, K;
	scanf("%d%d%d", &N, &M, &K);
	for (int i = 0; i < M; i++)
	{
    
    
		scanf("%d", &sch[i]);
	}
	for (int i = 0; i < N; i++)
	{
    
    
		grade[i].number = i;
		int ge, gt;
		scanf("%d%d", &ge, &gt);
		grade[i].GE = ge;
		grade[i].All = ge + gt;
		temp[i].GE = ge;
		temp[i].All = ge + gt;
		for (int j = 0; j < K; j++)
		{
    
    
			scanf("%d", &grade[i].ch[j]);
		}
	}
	qsort(grade, N, sizeof(grade[0]), cmp);
	for (int i = 0; i < N; i++)
	{
    
    
		for (int j = 0; j < K; j++)
		{
    
    
			if (sch[grade[i].ch[j]] > 0)
			{
    
    
				sc[grade[i].ch[j]].push_back(grade[i].number);
				sch[grade[i].ch[j]]--;
				break;
			}
			else if (sch[grade[i].ch[j]] == 0)
			{
    
    
				int s = sc[grade[i].ch[j]].size();
				int all = temp[sc[grade[i].ch[j]][s - 1]].All;
				int ge = temp[sc[grade[i].ch[j]][s - 1]].GE;
				if (grade[i].All == all and ge == grade[i].GE)
				{
    
    
					sc[grade[i].ch[j]].push_back(grade[i].number);
					break;
				}
				
			}
		}
	}
	for (int i = 0; i < M; i++)
	{
    
    
		sort(sc[i].begin(), sc[i].end());
		if (sc[i].size() == 0)
			printf("\n");
		else
		{
    
    
			printf("%d", sc[i][0]);
			for (int j = 1; j < sc[i].size(); j++)
			{
    
    
				printf(" %d", sc[i][j]);
			}
			printf("\n");
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_45478482/article/details/120084368