不同的是,在九度中“Each input file may contain more than one test case.”
所以借用PAT 1080中的代码,在main中加入循环,在循环开始调用init()函数初始化变量。
注意到九度好像不支持C++11 —— 我使用auto时给出了"warning: ‘auto’ will change meaning in C++0x; please remove it"
代码:
#include <cstdio> #include <vector> #include <algorithm> #include <list> #include <iostream> 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 (list<int>::iterator 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; } } } } void init() { for (int i = 0; i < n; ++ i) { applicant[i].m_choice.clear(); } for (int i = 0; i < m; ++ i) { school[i].clear(); } } int main() { while (cin >> n >> m >> k) { init(); 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; }