这一题我写了三个半小时。哈哈 哈哈 呸 想死的心都有了。
看了 gakkii 的文章 深受启发,才会出了这一道题
题目描述:
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 may contain more than 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>
using namespace std;
struct stu
{
int num;
int GE;
int GL;
int ave;
int select[5]; //志愿
int elected; //录取结果
};
struct school
{
int Quota; //名额
int ave; //平均分数线
int GE; //GE分数线
int amount;
};
bool cmp1(const stu a,const stu b)
{
if(a.ave!=b.ave)
return a.ave>b.ave; //当平均分不等时,按平均值排序(从大到小)
else
return a.GE>b.GE; //否则按GE
}
bool cmp2(const stu a,const stu b)
{
return a.num<b.num;
}
int main()
{
int n,m,k;
while(cin>>n>>m>>k)
{
school* mschool=new school[m];
stu* stud=new stu[n];
for(int i=0; i<m; i++) //读入每所学校名额
{
cin>>mschool[i].Quota;
mschool[i].ave=-1; //分数线
mschool[i].GE=-1; //分数线
mschool[i].amount=0;
}
for(int i=0; i<n; i++) //读入n个学生的信息
{
stud[i].num=i;
cin>>stud[i].GE>>stud[i].GL;
stud[i].ave=(stud[i].GE+stud[i].GL)/2;
for(int j=0; j<k; j++)
cin>>stud[i].select[j];
stud[i].elected=-1;
}
sort(stud,stud+n,cmp1); //按平均成绩和GE成绩排序
for(int i=0; i<n; i++) //第i名学生
{
for(int j=0; j<k; j++) //第j个志愿
{
int NUM=stud[i].select[j]; //用NUM记录第i名学生所志愿的大学
if(mschool[NUM].Quota>0)
{
if(mschool[NUM].Quota==1) //只有当school的名额为1是才记录该学生的成绩,作为录取分数线
{
mschool[NUM].ave=stud[i].ave;
mschool[NUM].GE=stud[i].GE;
}
stud[i].elected=NUM; //记录该学生的录取结果
mschool[NUM].Quota--;
mschool[NUM].amount++;
break;//啊啊啊!注意!!被录取后该同学应该跳出循环,不能再被其他大学录取
}
else if(mschool[NUM].Quota==0)
{
if(stud[i].ave==mschool[NUM].ave&&stud[i].GE==mschool[NUM].GE)
{
stud[i].elected=NUM;
mschool[NUM].amount++;
break;//啊啊啊!注意!!被录取后该同学应该跳出循环,不能再被其他大学录取
}
else
mschool[NUM].Quota--;
}
}
}
sort(stud,stud+n,cmp2);//为了升序输出
for(int i=0; i<m; i++)//输出结果
{
for(int j=0; j<n; j++)
{
if(stud[j].elected==i)
{
mschool[i].amount--;
if(mschool[i].amount!=0)
cout<<stud[j].num<<" ";
else
cout<<stud[j].num;
}
}
cout<<endl;
}
delete[] stud;
delete[] mschool;
}
return 0;
}
/**************************************************************
Problem: 1005
User: 晔晔
Language: C++
Result: Accepted
Time:0 ms
Memory:1524 kb
****************************************************************/