It is said that in 2013, there were 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:
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.
IDEA
1.code分为4部分:输入 排名 双向选择 输出
2.排名注意平均分与Ge均相同的学生排名相同
3.选择时双重循环实现双向选择
对于当前学生的某一志愿学校,若该学校招生额没满,则进入;若果满额(可能超),看该学生对于该学校招的最后一个学生排序是否相同:
若相同,则也进入该学校;若不同,进入该学生的下一个志愿学校
CODE
#include<iostream>
#include<cstdio>
#include<fstream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
#define MAX_m 110
#define MAX_n 40010
#define MAX_k 3
struct Stu{
int id;
int Ge,Gi;
double avg;
int rank;
int choice[MAX_k];
};
int cmp(Stu s1,Stu s2){
if(s1.avg==s2.avg){
return s1.Ge>s2.Ge;
}
return s1.avg>s2.avg;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif
//1 input
int n,m,k;
cin>>n>>m>>k;
int quota[MAX_m];
for(int i=0;i<m;i++){
cin>>quota[i];
}
vector<Stu> stu(n);
for(int i=0;i<n;i++){
stu[i].id=i;
cin>>stu[i].Ge>>stu[i].Gi;
stu[i].avg=(stu[i].Ge+stu[i].Gi)*(1.0)/2;
for(int j=0;j<k;j++){
cin>>stu[i].choice[j];
}
}
//2 排名 最后分数相同且Ge分数相同,则排名相同
sort(stu.begin(),stu.end(),cmp);
stu[0].rank=1;
map<int,int> rank_m;
rank_m[stu[0].id]=1;
for(int i=1;i<n;i++){
if(stu[i].avg==stu[i-1].avg&&stu[i].Ge==stu[i-1].Ge){
stu[i].rank=stu[i-1].rank;
}else{
stu[i].rank=i+1;
}
rank_m[stu[i].id]=stu[i].rank;//把排名存起来
}
// for(int i=0;i<n;i++){
// cout<<stu[i].id<<" "<<stu[i].rank<<" "<<stu[i].avg<<" "<<stu[i].Ge<<endl;
// }
//3 select
vector<int> result[MAX_m];
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
int sch_id=stu[i].choice[j];
if(quota[sch_id]==0){
continue;
}
if(result[sch_id].size()<quota[sch_id]){//该学校没有招够人数,则可以进入该校
result[sch_id].push_back(stu[i].id);
break;
}else{
//若果招生满了或者超了,看随后一个学生的排序与当前学生排名时候相同
int laststu=result[sch_id][result[sch_id].size()-1];//最后一个学生
if(rank_m[laststu]==stu[i].rank){//如果该学校最后一个学生排名与当前学生排名相同且招生人数满额,也要进入该学校
result[sch_id].push_back(stu[i].id);
break;
}
else{
continue;//否则该学生选择下一志愿学校
}
}
}
}
//4 output
for(int i=0;i<m;i++){
sort(result[i].begin(),result[i].end());
vector<int>::iterator it;
for(it=result[i].begin();it!=result[i].end();it++){
if(it==result[i].begin()){
cout<<*it;
}else{
cout<<" "<<*it;
}
}
cout<<endl;
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}