a1080 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.
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef struct Applicant
{
int ge, gi, sum;
int stu_id;
int choices[5];
int rank;
}applicant;
applicant app[40010];
void init_stu(int n)
{
for (int i = 0;i < n;i++)
app[i].stu_id = i;
}
//初始化app数组
typedef struct School
{
int quota; //招生额度
int stuNum; //当前实际招生人数
int lastAdmit; //最后一个录取的学生ID
int id[40010]; //录取的学生编号
}school;
school sch[110];
void init_school(int m)
{
for (int i = 0;i < m;i++)
{
sch[i].stuNum = 0;
sch[i].lastAdmit = -1;
}
}
//初始化sch数组
bool cmp(applicant a, applicant b)
{
if (a.sum != b.sum)
return a.sum > b.sum;
else
return a.ge > b.ge;
}
bool cmp_id(int a, int b)
{
return a < b;
}
int main()
{
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
init_stu(n);
init_school(m);
int i,j;
for (i = 0;i < m;i++)
scanf("%d", &sch[i].quota);
for (i = 0;i < n;i++)
{
scanf("%d %d", &app[i].ge, &app[i].gi);
for (j = 0;j < k;j++)
scanf("%d", &app[i].choices[j]);
app[i].sum = app[i].ge + app[i].gi;
}
sort(app, app + n, cmp);
app[0].rank = 1;
for (i = 1;i < n;i++)
{
if (app[i].sum == app[i - 1].sum&&app[i].ge == app[i - 1].ge)
app[i].rank = app[i - 1].rank;
else
app[i].rank = i + 1;
}
for (i = 0;i < n;i++)
{
for (j = 0;j < k;j++)
{
int num = app[i].choices[j];
int last=sch[num].lastAdmit;
int stu_num=sch[num].stuNum;
if (stu_num < sch[num].quota||(last!=-1&& app[i].rank == app[last].rank))
{
sch[num].id[stu_num] = app[i].stu_id;
sch[num].stuNum++;
sch[num].lastAdmit = app[i].stu_id;
break;
}
}
}
for (i = 0;i < m;i++)
{
if (sch[i].stuNum > 0)
{
int num = sch[i].stuNum;
sort(sch[i].id, sch[i].id + num, cmp_id);
for (j = 0;j < num;j++)
{
printf("%d", sch[i].id[j]);
if (j < num - 1)
printf(" ");
}
}
printf("\n");
}
return 0;
}
运行结果:
部分正确 26/30