I haven't updated my blog for a long time. It's a sin...
The last question of the 2011 server trial of Zhejiang University was found on OJ for a long time.
I think this question is very watery when talking about the Internet. As a result, I made a long time. It seems that I am not a question, but I am too watery...
In fact, the question is that there are more conditions and there is no complicated one.Algorithm, Mainly stuck in the processing of conditions. The original idea is divided into three situations (arrays are sorted first)
1. The applicant's final score does not match the next applicant's final score
2. The applicant's final score is equal to the next applicant's final score, but the entrance exam score does not match
3. the final score is equal, and the admission score is equal.
Handle these three situations in sequence, and find the corresponding school according to each person's choice...
In fact, it would be much simpler from the perspective of the school (refer to this Article)Article)
1. The school selected by the applicant is still available
2. The number of applicants for admission to the school is full, but there is the same rank as the last one.
A small trick is to sort applicants in descending order based on final scores and admission scores.
The final output also took a lot of time, because the fast sorting is unstable, so the final output had to be sorted.
Below is what I wrote:Code...
# Include <stdio. h> # include <stdlib. h> # define n 40000 # define K 5 typedef struct {int ID; int Ge; int gi; int final; int choice [k];} applicant; typestrudef CT {int sum; int limit; int P [N];} school; int CMP (const void * P, const void * q) {applicant * A = (applicant *) P; applicant * B = (applicant *) q; if (a-> final! = B-> final) return B-> final-a-> final; else return B-> Ge-a-> Ge;} int cmp2 (const void * P, const void * q) {return * (int *) P-* (int *) q;} int main () {int N, K, M; int I, J; applicant * app; school * Sch; int t; while (scanf ("% d", & N, & M, & K )! = EOF) {Sch = (school *) malloc (M * sizeof (school); for (I = 0; I <m; I ++) {scanf ("% d", & Sch [I]. limit); Sch [I]. sum = 0;} APP = (applicant *) malloc (N * sizeof (Applicant); for (I = 0; I <n; I ++) {scanf ("% d", & App [I]. GE, & App [I]. gi); app [I]. id = I; app [I]. final = (APP [I]. ge + app [I]. gi)/2; for (j = 0; j <K; j ++) scanf ("% d", & App [I]. choice [J]);} qsort (app, N, sizeof (applicant), CMP); for (I = 0; I <n; I ++) {for (j = 0; j <K; j ++) {T = app [I]. choice [J]; If (Sch [T]. sum <Sch [T]. limit) {Sch [T]. P [Sch [T]. sum ++] = I; break;} else if (APP [Sch [T]. P [Sch [T]. sum-1]. final = app [I]. final & App [Sch [T]. P [Sch [T]. sum-1]. GE = app [I]. ge) {Sch [T]. P [Sch [T]. sum ++] = I; break ;}}for (I = 0; I <m; I ++) {for (j = 0; j <Sch [I]. SUM; j ++) Sch [I]. P [J] = app [Sch [I]. P [J]. ID; qsort (Sch [I]. p, Sch [I]. sum, sizeof (INT), cmp2); For (j = 0; j <Sch [I]. sum-1; j ++) {printf ("% d", Sch [I]. P [J]);} If (j <Sch [I]. sum) printf ("% d \ n", Sch [I]. P [J]); elseputs ("");} Free (Sch); free (APP);} return 0 ;}