学生总分高的排名在前;
总分相同,GE得分高的排名在前;
GE相同,则排名相同。
注意到,编写<algorithm>里的sort函数所调用的比较函数不能在前两个条件相等时,直接返回true.
知道原理的告诉我声... 我是改成了a.m_number < b.m_number, 即比较学生的id.
friend bool operator< (const Applicant& a, const Applicant& b) { int aa = a.m_ge + a.m_gi; int bb = b.m_ge + b.m_gi; if (aa != bb) { return aa > bb; } else if (a.m_ge != b.m_ge) { return a.m_ge > b.m_ge; } else { return a.m_number < b.m_number; ////// true } }
代码:
#include <cstdio> #include <vector> #include <algorithm> #include <list> using namespace std; struct Applicant { int m_number; int m_ge; int m_gi; list<int> m_choice; friend bool operator== (const Applicant& a, const Applicant& b) { return a.m_ge == b.m_ge && a.m_gi == b.m_gi; } friend bool operator< (const Applicant& a, const Applicant& b) { int aa = a.m_ge + a.m_gi; int bb = b.m_ge + b.m_gi; if (aa != bb) { return aa > bb; } else if (a.m_ge != b.m_ge) { return a.m_ge > b.m_ge; } else { return a.m_number < b.m_number; ////// true } } }; Applicant applicant[40010]; vector<int> school[110]; int n, m, k, quota[110]; int capacity, ge, gi, choice; void choose_school(int begin, int end) { bool full[110]; for (int i = 0; i < m; ++ i) { full[i] = school[i].size() >= quota[i]; } for (int i = begin; i < end; ++ i) { for (auto it = applicant[i].m_choice.begin(); it != applicant[i].m_choice.end(); ++ it) { if (full[*it] == false) { school[*it].push_back( applicant[i].m_number ); break; } } } } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; ++ i) { scanf("%d", quota + i); } for (int i = 0; i < n; ++ i) { applicant[i].m_number = i; scanf("%d%d", &applicant[i].m_ge, &applicant[i].m_gi); for (int j = 0; j < k; ++ j) { scanf("%d", &choice); applicant[i].m_choice.push_back(choice); } } sort(applicant, applicant+n); for (int i = 0; i < n; ) { int begin = i; ++ i; while (i < n && applicant[i] == applicant[begin]) { ++ i; } choose_school(begin, i); } for (int i = 0; i < m; ++ i) { sort (school[i].begin(), school[i].end()); bool first = true; for (size_t j = 0; j < school[i].size(); ++ j) { if (first) { printf("%d", school[i][j]); first = false; } else { printf(" %d", school[i][j]); } } printf("\n"); } return 0; }