九度oj 1005

题目1005:Graduate Admission            

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:6182

解决:1791

题目描述:                       

    It is said that in 2011, there are 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.

输入:                       

    Each input file may contain more than 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.

输出:                       

    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.

样例输入:                       
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
样例输出:                       
0 10
3
5 6 7
2 8

1 4

此题坑比较多
第一,输出时每行末尾不能有空格
第二,输出时如果某学校未录取学生则为空行
第三,输出时每个学校录取的学生按从小到大输出

代码如下:
  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <algorithm>
  4 using namespace std;
  5 
  6 struct Applicant{
  7   int GE;
  8   int GI;
  9   int choices[6];
 10   int num;
 11 };
 12 
 13 /*int compare(const void *x0, const void *y0)
 14 {
 15     Applicant x = *(Applicant *)x0;
 16     Applicant y = *(Applicant *)y0;
 17     int gradeX = x.GE + x.GI;
 18     int gradeY = y.GE + y.GI;
 19     if(gradeX > gradeY) {
 20       return -1;
 21     }
 22     else if(gradeX < gradeY) {
 23       return 1;
 24     }
 25     else {
 26       if(x.GE > y.GE) {
 27         return -1;
 28       }
 29       else if(x.GE < y.GE) {
 30           return 1;
 31       }
 32       else {
 33         return 0;
 34       }
 35     }
 36 }*/
 37 
 38 int compare(const void *x0, const void *y0)
 39 {
 40     Applicant x = *(Applicant *)x0;
 41     Applicant y = *(Applicant *)y0;
 42     int gradeX = x.GE + x.GI;
 43     int gradeY = y.GE + y.GI;
 44     if(gradeX == gradeY) {
 45         return y.GE > x.GE;
 46     }
 47     else {
 48         return gradeY > gradeX;
 49     }
 50 }
 51 
 52 int compare2(const void *x0, const void *y0) {
 53     return *(int *)x0 - *(int *)y0;
 54 }
 55 
 56 bool Equal(Applicant a,Applicant b)
 57 {
 58     if(a.GE==b.GE&&a.GI==b.GI)
 59         return true;
 60     return false;
 61 }
 62 
 63 Applicant apply[40002];
 64 int ok[102][40002];
 65 Applicant score[102];
 66 
 67 int main() {
 68     int M, N, K;
 69     //N <= 40000 M <= 100 K <= 5
 70     int quota[102];
 71     int man[102];
 72     //freopen("input.txt","r",stdin);
 73     while(scanf("%d %d %d",&N, &M, &K) != EOF) {
 74         for(int i = 0; i < M; i++) {
 75           scanf("%d",&quota[i]);
 76           man[i] = 0;
 77         }
 78         for(int i = 0; i < N; i++) {
 79           scanf("%d %d",&apply[i].GE, &apply[i].GI);
 80           for(int j = 0; j < K; j++) {
 81             scanf("%d",&apply[i].choices[j]);
 82           }
 83           apply[i].num = i;
 84         }
 85         //have read Matrix
 86         qsort(apply, N, sizeof(Applicant),compare);
 87         //sort
 88         
 89        /* printf("
");
 90         for(int i = 0; i < N; i++) {
 91           printf("%d %d ",apply[i].GE, apply[i].GI);
 92           for(int j = 0; j < K; j++) {
 93             printf("%d ",apply[i].choices[j]);
 94           }
 95           printf("
");
 96         }
 97         printf("
");*/
 98         
 99         for(int i = 0; i < N; i++) {
100           for(int j = 0; j < K; j++) {
101             int school = apply[i].choices[j];
102             if(quota[school] == 0) {
103               continue;//next choice
104             }
105             if(man[school] < quota[school]) {
106                 ok[school][man[school]] = apply[i].num;
107                 score[school].GE = apply[i].GE;
108                 score[school].GI=apply[i].GI;
109                 man[school]++;
110                 break;//next person
111             }
112             else if(man[school] > 0){
113               int lastS = ok[school][man[school]-1];
114               //此处lastS记录的是num,是原来的顺序
115               //而此时数组已经排序,换了一个顺序,所以用lastS就会出错 
116               bool flag = Equal(apply[i],score[school]);
117               if(flag == true) {
118                   //puts("ok");
119                 ok[school][man[school]] = apply[i].num;
120                 score[school].GE = apply[i].GE;
121                 score[school].GI=apply[i].GI;
122                 man[school]++;
123                 break;//next person
124               }
125               
126             }
127           }
128         }
129         for(int i = 0; i < M; i++) {
130           qsort(ok[i],man[i],sizeof(int),compare2);
131           for(int k = 0; k < man[i] - 1; k++) {
132               printf("%d ",ok[i][k]);
133           }
134           if(man[i] != 0) {
135               printf("%d",ok[i][man[i]-1]);
136           }
137           printf("
");
138         }
139     } 
140     return 0;
141 }

这到题做错了多次,开始令我百思不得其解。经过反复检查,发现出错点在与79行的lastS,开始比较Equal用的是apply[i]和apply[lastS],但此处lastS是排序前的num值,并不是排序后数组的下标。后来去记录每一学校最后录取一人的成绩,问题得以解决。

原文地址:https://www.cnblogs.com/jasonJie/p/5651444.html