It is said that in 2013, there were about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admission procedure.
Each applicant will have to provide two grades: the national entrance exam grade GE, and the interview grade GI. The final grade of an applicant is (GE + GI) / 2. The admission rules are:
- The applicants are ranked according to their final grades, and will be admitted one by one from the top of the rank list.
- If there is a tied final grade, the applicants will be ranked according to their national entrance exam grade GE. If still tied, their ranks must be the same.
- Each applicant may have K choices and the admission will be done according to his/her choices: if according to the rank list, it is one's turn to be admitted; and if the quota of one's most preferred shcool is not exceeded, then one will be admitted to this school, or one's other choices will be considered one by one in order. If one gets rejected by all of preferred schools, then this unfortunate applicant will be rejected.
- If there is a tied rank, and if the corresponding applicants are applying to the same school, then that school must admit all the applicants with the same rank, even if its quota will be exceeded.
Input Specification:
Each input file contains one test case. Each case starts with a line containing three positive integers: N (<=40,000), the total number of applicants; M (<=100), the total number of graduate schools; and K (<=5), the number of choices an applicant may have.
In the next line, separated by a space, there are M positive integers. The i-th integer is the quota of the i-th graduate school respectively.
Then N lines follow, each contains 2+K integers separated by a space. The first 2 integers are the applicant's GE and GI, respectively. The next K integers represent the preferred schools. For the sake of simplicity, we assume that the schools are numbered from 0 to M-1, and the applicants are numbered from 0 to N-1.
Output Specification:
For each test case you should output the admission results for all the graduate schools. The results of each school must occupy a line, which contains the applicants' numbers that school admits. The numbers must be in increasing order and be separated by a space. There must be no extra space at the end of each line. If no applicant is admitted by a school, you must output an empty line correspondingly.
Sample Input:
11 6 3 2 1 2 2 2 3 100 100 0 1 2 60 60 2 3 5 100 90 0 3 4 90 100 1 2 0 90 90 5 1 3 80 90 1 0 2 80 80 0 1 2 80 80 0 1 2 80 70 1 3 2 70 80 1 2 3 100 100 0 2 4
Sample Output:
0 10 3 5 6 7 2 8 1 4
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 typedef struct{ 6 int id; 7 int Ge; 8 int Gi; 9 int choice[6]; 10 int rank; 11 int school; 12 }info; 13 bool cmp(info a, info b){ 14 int suma = a.Ge + a.Gi, sumb = b.Ge + b.Gi; 15 if(suma != sumb) 16 return suma > sumb; 17 else return a.Ge > b.Ge; 18 } 19 bool cmp2(info a, info b){ 20 if(a.school != b.school) 21 return a.school < b.school; 22 else 23 return a.id < b.id; 24 } 25 info stu[40001]; 26 int main(){ 27 int N, M, K, quota[101]; 28 scanf("%d%d%d", &N, &M, &K); 29 for(int i = 0; i < M; i++) 30 scanf("%d", "a[i]); 31 for(int i = 0; i < N; i++){ 32 stu[i].id = i; 33 stu[i].school = -1; 34 scanf("%d%d", &stu[i].Ge, &stu[i].Gi); 35 for(int j = 0; j < K; j++) 36 scanf("%d", &stu[i].choice[j]); 37 } 38 sort(stu, stu + N, cmp); 39 stu[0].rank = 1; 40 stu[0].school = stu[0].choice[0]; 41 quota[stu[0].school]--; 42 for(int i = 1; i < N; i++){ 43 if(stu[i].Ge + stu[i].Gi == stu[i - 1].Ge + stu[i - 1].Gi && stu[i].Ge == stu[i - 1].Ge){ 44 stu[i].rank = stu[i - 1].rank; 45 }else{ 46 stu[i].rank = i + 1; 47 } 48 for(int j = 0; j < K; j++){ 49 if(quota[stu[i].choice[j]] > 0){ 50 stu[i].school = stu[i].choice[j]; 51 quota[stu[i].choice[j]]--; 52 break; 53 }else if(quota[stu[i].choice[j]] <= 0 && stu[i].choice[j] == stu[i - 1].school && stu[i].rank == stu[i - 1].rank){ 54 stu[i].school = stu[i].choice[j]; 55 quota[stu[i].choice[j]]--; 56 break; 57 } 58 } 59 } 60 sort(stu, stu + N, cmp2); 61 int index, cnt = 0; 62 for(int i = 0; i < N; i++){ 63 index = i; 64 if(stu[i].school != -1) 65 break; 66 } 67 while(cnt < stu[index].school){ 68 printf(" "); 69 cnt++; 70 } 71 printf("%d", stu[index].id); 72 int temp = stu[index].school; 73 for(int i = index + 1; i < N; i++){ 74 if(stu[i].school == temp){ 75 printf(" %d", stu[i].id); 76 }else{ 77 temp = stu[i].school; 78 while(cnt < temp){ 79 printf(" "); 80 cnt++; 81 } 82 printf("%d", stu[i].id); 83 } 84 } 85 while(cnt < M){ 86 printf(" "); 87 cnt++; 88 } 89 cin >> N; 90 return 0; 91 }
总结:
1、这道题模拟的是填报志愿录取的情况。基本思路是先按照学生的平均分(总分)进行排序,然后确定排名。然后从第一名开始陆续处理志愿学校,处理完第i名后才能处理第i+1名。要注意的是,当二人的名次相同但只有一个录取名额时,不需要考虑名额限制而要将二人同时录取。在解题中体现的就是:顺序查看第i人的K个志愿,当他的第j志愿学校名额充足时,正常录取;当第i人的第j个志愿学校已经没有名额时,如果该人的名次和他前面一人的名次相同,且他的第j个志愿和前一人的最终录取学校相同时,这个第i人可以被j志愿录取。
2、在处理完所有人的录取流程后,还无法直接输出。题目要求以学校为主进行输出。此时,如果设置M维数组记录M个学校分别录取的学生的id,还需要按id排序,将要遍历很多次stu数组,很可能超时。 这时可以再进行一次排序,按照学校序号的大小和学生的id综合排序,得到学校相同的学生在一起,且按照学生id从小到大排列。这时就可以输出了,有些学校没有人录取,只输出一个空行,需要在按照stu数组遍历时多加注意(需遍历到所有M个学校而非仅仅stu里的N个学生)。
3、由上可知,可以利用排序算法进行分类(当正常分类复杂度很高时)。