按照给定规则排好序,从头到尾进行录取,注意要同时录取排名相同的所有学生.每个学校录取的人数可能超过给定的上限.
这种题需要静下心来慢慢做,不能浮躁
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;
vector<int> quotas;
map<int,vector<int >> schToStu;
int N,M,K;
struct Person{
int rank;
int id;
int Gi;
int Ge;
vector<int> prefers;
};
vector<Person*> persons;
bool cmp(Person* person1,Person* person2){
if (person1->Gi+person1->Ge==person2->Gi+person2->Ge){
return person1->Ge>person2->Ge;
}
return person1->Gi+person1->Ge>person2->Gi+person2->Ge;
}
void judge(vector<Person*>& pers, bool* hasJob){
for (int i = 0; i < K; ++i) {
map<int,vector<Person*>> map1;
for (auto & per : pers) {
if (!hasJob[per->id]) {
map1[per->prefers[i]].push_back(per);
}
}
for (const auto& pair:map1){
int school=pair.first;
if (quotas[school]>0){//有一个指标就行
quotas[school]-=pair.second.size();
for(auto x:pair.second){
schToStu[school].push_back(x->id);
hasJob[x->id]=true;
}
}
}
}
}
int main() {
cin>>N>>M>>K;
bool hasJob[N];
memset(hasJob, false, sizeof(hasJob));
for (int i = 0; i < M; ++i) {
int val;
cin>>val;
quotas.push_back(val);
}
for (int i = 0; i < N; ++i) {
auto* person=new Person();
person->id=i;
cin>>person->Ge>>person->Gi;
for (int j = 0; j < K; ++j) {
int val;
cin>>val;
person->prefers.push_back(val);
}
persons.push_back(person);
}
sort(persons.begin(),persons.end(),cmp);
int rank=1;
persons[0]->rank=1;
for (int i = 1; i < persons.size(); ++i) {
rank++;
if (persons[i]->Ge==persons[i-1]->Ge&&persons[i]->Gi==persons[i-1]->Gi){
persons[i]->rank=persons[i-1]->rank;
} else{
persons[i]->rank=rank;
}
}
//从头开始录取
rank=1;
int preRank=rank;
vector<Person*> pers;
pers.push_back(persons[0]);
for (int i = 1; i < N; ++i) {
if (persons[i]->rank==preRank){
pers.push_back(persons[i]);
} else{
judge(pers,hasJob);
pers.clear();
preRank=persons[i]->rank;
pers.push_back(persons[i]);
}
}
judge(pers,hasJob);
for (int i = 0; i < M; ++i) {
if (schToStu[i].empty()){
cout<<endl;
continue;
}
sort(schToStu[i].begin(),schToStu[i].end());
cout<<schToStu[i][0];
for (int j = 1; j < schToStu[i].size(); ++j) {
cout<<" "<<schToStu[i][j];
}
cout<<endl;
}
return 0;
}