题解 | #Graduate Admission#
Graduate Admission
https://www.nowcoder.com/practice/b1bf27f86ecd4a5d9720d9cdc00d178e
#include <iostream> #include<algorithm> #include <vector> using namespace std; struct ApplicantInfo{ int id; int rank; int GE; int GI; double Average; ApplicantInfo(int _id,int _GE,int _GI,double _Average){ id = _id; GE = _GE; GI = _GI; Average = _Average; } }; bool cmp(ApplicantInfo a,ApplicantInfo b){ if(a.Average>b.Average) return true; else if(a.Average==b.Average){ if(a.GE>b.GE) return true; } return false; } bool cmpid(ApplicantInfo a,ApplicantInfo b){ return a.id<b.id; } int main() { int N,M,K; while (cin >> N >> M >> K) { // 注意 while 处理多个 case vector<int> schoolQuotaNumber(M);//学校最多招收人数 vector<vector<ApplicantInfo>> schoolApplicant(M);//某学校录取学生的信息 vector<vector<int>> ApplicantChoice(N);//某学生的学校选择 vector<ApplicantInfo> info;//学生信息 //信息处理 for(int i=0;i<M;i++) cin>>schoolQuotaNumber[i]; for(int i=0;i<N;i++){ int ge,gi,choice; cin>>ge>>gi; for(int j=0;j<K;j++){ cin>>choice; ApplicantChoice[i].emplace_back(choice); } info.emplace_back(i,ge,gi,(ge*1.0+gi*1.0)/2); } //根据成绩排序 sort(info.begin(),info.end(),cmp); //计算排名 info[0].rank=1; int count=1; for(int i=1;i<N;i++){ count++; if(info[i].Average<info[i-1].Average){ info[i].rank= count;//平均分不相等排名为前面的人数+1 }else{ if(info[i].GE<info[i-1].GE){ info[i].rank = count; }else{//平均分,GE分全相等排名相等 info[i].rank = info[i-1].rank; } } } //根据排名录取 for(int i=0;i<N;i++){ int rank = info[i].rank; int id = info[i].id; for(int j=0;j<ApplicantChoice[id].size();j++){ int choice = ApplicantChoice[id][j]; if(schoolQuotaNumber[choice]>0){ schoolApplicant[choice].emplace_back(info[i]); schoolQuotaNumber[choice]--; break;//只能被一个学校录取 }else{ int size = schoolApplicant[choice].size();//该学校已经录取的人数 int thelastrank = schoolApplicant[choice][size-1].rank; if(thelastrank==rank) //和最后一名排名一样就算人数已满也要招进去 { schoolApplicant[choice].emplace_back(info[i]);//录取进这个学校 break;//只能被一个学校录取 } } } } //按照id排序 for(int i=0;i<M;i++) sort(schoolApplicant[i].begin(),schoolApplicant[i].end(),cmpid); //输出 for(int i=0;i<M;i++){ int size = schoolApplicant[i].size(); for(int j=0;j<size;j++){ if(j!=0) cout<<" "; cout<<schoolApplicant[i][j].id; } if(i!=M-1) cout<<endl; } } } // 64 位输出请用 printf("%lld")