首页 > 代码库 > PAT 1080
PAT 1080
排序题目,搞混了一件事情,那就是,排序结束之后顺序是变的啊亲... 很丑地解决了问题,其实应该对每一个学校维护一个last applicant比较,不过能过就行了
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5 using namespace std; 6 7 struct Applicant{ 8 int ge; 9 int gi;10 int score;11 12 int idx;13 14 vector<int> apply_school;15 };16 17 struct sort_op{18 bool operator()(Applicant a1, Applicant a2){19 if (a1.score != a2.score)20 return (a1.score > a2.score);21 return (a1.ge > a2.ge);22 }23 };24 25 int main(){26 int N, M, K;27 cin >> N >> M >> K;28 29 vector<Applicant> applicants(N);30 vector<vector<int> > school_res(M);31 vector<int> school_quato(M);32 33 for (int i = 0; i < M; i++){34 int cnt;35 cin >> cnt;36 school_quato[i] = cnt;37 }38 39 for (int i = 0; i < N; i++){40 int ge, gi, idx;41 cin >> ge >> gi;42 43 applicants[i].ge = ge;44 applicants[i].gi = gi;45 applicants[i].score = ge + gi;46 applicants[i].idx = i;47 48 for (int j = 0; j < K; j++){49 cin >> idx;50 applicants[i].apply_school.push_back(idx);51 }52 }53 54 vector<Applicant> bck_up = applicants;55 sort(applicants.begin(), applicants.end(), sort_op());56 57 for (int i = 0; i < N; i++){58 for (int j = 0; j < K; j++){59 int school_idx = applicants[i].apply_school[j];60 if (school_quato[school_idx] > 0){61 school_quato[school_idx]--;62 school_res[school_idx].push_back(applicants[i].idx);63 64 break;65 } else {66 if (school_res[school_idx].size() == 0) continue;67 int last = school_res[school_idx].back();68 if (applicants[i].gi == bck_up[last].gi && applicants[i].ge == bck_up[last].ge){69 school_res[school_idx].push_back(applicants[i].idx);70 school_quato[school_idx]--;71 break;72 }73 }74 }75 }76 77 for (int i = 0; i < school_res.size(); i++){78 if (school_res[i].size() != 0){79 sort(school_res[i].begin(), school_res[i].end());80 cout << school_res[i][0];81 82 for (int j = 1; j < school_res[i].size(); j++)83 cout << " " << school_res[i][j];84 }85 86 cout << endl;87 }88 89 return 0;90 }
PAT 1080
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。