PAT甲级 1080 Graduate Admission

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 (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 GEand 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

解题思路及变量解释:

  1. 本题用到了两个结构体,struct applicant用于存储候选人相关消息其中rank表示成绩在所有人中的排名。struct school用于存储学校相关信息,其中accept数组用于记录被录取学生的学号,初值为0。rank表示已录取学生的最低排名初值为0。

  2. 判断是否被录取主要分为两步。第一步判断当前学生的排名是否等于心仪学校录取的最低排名,相等则录取。若不相等则判断心仪学校的配额是否大于0,大于0则录取并将当前学校的排名赋值给心仪学校的最低录取排名。如下:

if(a[i].rank == b[a[i].prefer_school[j]].rank)
{}
else if(b[a[i].prefer_school[j]].quota > 0)
{}
  1. a[i].prefer_school[j]表示第i个学生的第j个心仪学校。
  2. 学校接受的学生的学生要表示为accept[a[i].ID],因为排序后结构体数组的下标已经不是学生的ID号。

代码:

#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
struct applicant
{
	int ID; 
	int gradeE;
	int gradeI;
	int prefer_school[5];
	int rank;
}a[50000]; 
struct school
{
	int quota;
	int rank;
	int people;
	int accept[50000];
}b[100];
bool cmp(struct applicant m,struct applicant n)
{
	if(m.gradeE+m.gradeI!=n.gradeE+n.gradeI)
		return m.gradeE+m.gradeI > n.gradeE+n.gradeI;
	else 
		return m.gradeE > n.gradeE;
}
int main(void)
{
	int N,M,K;
	scanf("%d %d %d",&N,&M,&K);
	for(int i = 0;i < M;i++)
	{
		scanf("%d",&b[i].quota);
		b[i].rank = 0;
		b[i].people = 0;
		memset(b[i].accept,0,sizeof(b[i].accept));
	}
	for(int i = 0;i < N;i++)
	{
		scanf("%d %d",&a[i].gradeE,&a[i].gradeI);
		a[i].ID = i;
		for(int j = 0;j < K;j++)
		{
			scanf("%d",&a[i].prefer_school[j]);
		}
	}
	sort(a,a+N,cmp);
	a[0].rank = 1;
	for(int i = 0;i < N;i++)
	{
		if(a[i].gradeE + a[i].gradeI!=a[i-1].gradeE+a[i-1].gradeI||(a[i].gradeE + a[i].gradeI==a[i-1].gradeE+a[i-1].gradeI)&&a[i].gradeE!=a[i-1].gradeE)
			a[i].rank = i + 1;
		else
			a[i].rank = a[i - 1].rank;
		for(int j = 0;j < K;j++)
		{
			if(a[i].rank == b[a[i].prefer_school[j]].rank)
			{
				b[a[i].prefer_school[j]].accept[a[i].ID] = 1;
				b[a[i].prefer_school[j]].people++;
				b[a[i].prefer_school[j]].quota--;
				break;
			}
			else if(b[a[i].prefer_school[j]].quota > 0)
			
			{
				b[a[i].prefer_school[j]].accept[a[i].ID] = 1;
				b[a[i].prefer_school[j]].quota--;
				b[a[i].prefer_school[j]].people++;
				b[a[i].prefer_school[j]].rank = a[i].rank;
				break;
			}
		}
	}
	for(int i = 0;i < M;i++)
	{
		if(b[i].people != 0)
		{
			for(int j = 0,m = 0;m < b[i].people;j++)
			{
				if(b[i].accept[j] == 1)
				{
					printf("%d",j); 
					m++;
					if(m != b[i].people)
					{
						printf(" ");
					}
				}
			}
			printf("\n");
		}
		else
			printf("\n");
	}
		return 0;
}

第55-58行判断语句比较长可以换一下逻辑缩短代码:

if((a[i].gradeE + a[i].gradeI==a[i-1].gradeE+a[i-1].gradeI)&&(a[i].gradeE == a[i - 1].gradeE))
			a[i].rank = a[i - 1].rank;
		else
			a[i].rank = i + 1;
发布了6 篇原创文章 · 获赞 1 · 访问量 207

猜你喜欢

转载自blog.csdn.net/lovingcgq/article/details/104138668