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.
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 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.
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.
题目大意:每个考生有两个成绩:GE和GI,最终成绩为(GE + GI) / 2;按照最终成绩排名,如果最终成绩相同,就按照GE排名,如果仍然相同,他们的排名就是相同的。每个申请者有K个选择院校,每个学校也有招生人数限制。按照排名先后,如果当前考生的第一个志愿学校的名额还没满,就录取进去;如果当前志愿名额满了但是该校最后一个录取的人的排名和当前考生相同,则不管招生人数限制,依旧应该被录取;否则考虑该生的下一个志愿。如果所有志愿都没有能被录取,则该生落榜。学校编号0~m,学生编号0~n,最后让你输出每个学校的录取情况。
分析:属于模拟题。
1)信息录入:
开一个结构体stu,属性有ge,gi,id,fin,choice,由于要对结构体排序,那就可以在创建结构体时进行运算符重载。
struct node{
int ge,gi,id,fin;
vector<int>choice;
bool operator <(const node & a)const{
return fin!=a.fin? fin >a.fin: ge>a.ge;
}
};
然后读入输入信息即可。题目里面给出fin=(ge+gi)/2,这里我们不进行/2运算,因为fin=(ge+gi)/2 与 fin=ge+gi排序效果相同,且后者不会出现精度差。
2)录取程序模拟;
排好序后,扫描每一个人,选出他的心仪学校,如果学校有名额,那就直接分配名额;若没有名额且他与心意学校录取的上一个学生条件相同(stu[i].fin==lastgrade[schid] &&lastge[schid]==stu[i].ge),则进行超额录取。注意:每个学生只能被一个学校录取,记得及时break。
完整代码:
#include<bits/stdc++.h>
using namespace std;
struct node{
int ge,gi,id,fin;
vector<int>choice;
bool operator <(const node & a)const{
return fin!=a.fin? fin >a.fin: ge>a.ge;
}
};
int main(){
int n,m,k;
cin>>n>>m>>k;
vector<int> num(m),school[m],lastgrade(m,-1),lastge(m,-1);
vector<node>stu(n);
for(int i=0;i<m;i++) cin>>num[i]; //录入i学校的录取人数 num[i]
for(int i=0;i<n;i++){
cin>>stu[i].ge>>stu[i].gi;
stu[i].fin=stu[i].ge+stu[i].gi;
stu[i].id=i;
stu[i].choice.resize(k);
for(int j=0;j<k;j++){
cin>>stu[i].choice[j];
}
}
sort(stu.begin(),stu.end());
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
int schid=stu[i].choice[j]; //第 i个学生的心仪学校
if(num[schid]){ //若学校还有名额
num[schid]--;
school[schid].push_back(stu[i].id);
// school[schid].push_back(i); 这样为什会出错 ?????
lastgrade[schid]=stu[i].fin;
lastge[schid]=stu[i].ge;
break; //一个学生只能录取到一个学校
}else if ((stu[i].fin==lastgrade[schid] )&& lastge[schid]==stu[i].ge){
school[schid].push_back(stu[i].id);break;
}
}
}
for(int i=0;i<m;i++){
sort(school[i].begin(),school[i].end());
for(int j=0;j<school[i].size();j++){
printf("%d%s",school[i][j],j!=school[i].size()-1? " ":"");
}
cout<<endl;
}
return 0;
}
有一处疑问?
school[schid].push_back(stu[i].id);
school[schid].push_back(i); 这样为什会出错 ?????
为什么再将学生编号放入学校录取名单时第二行代码会出错,明明在之前信息录入时已经stu【i】.id = i 了???
That“s all !