1080 Graduate Admission 30 分
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 ?? , and the interview grade . The final grade of an applicant is . 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 . 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 integers separated by a space. The first 2 integers are the applicant's and , respectively. The next integers represent the preferred schools. For the sake of simplicity, we assume that the schools are numbered from 0 to , and the applicants are numbered from to .
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
思路
- 建立一個(gè)結(jié)構(gòu)體用于存儲(chǔ)學(xué)生的信息,建立一個(gè)結(jié)構(gòu)體數(shù)組存儲(chǔ)所有的學(xué)生癞埠。
struct student
{
int ge;
int gi;
int choices[maxk]; // 記錄學(xué)生的志愿效斑。
int key; // 記錄學(xué)生的id珍语,因?yàn)橹髸?huì)進(jìn)行排序顽铸,學(xué)生的索引便不再是他們的id了端盆。
} Stu[maxn];
- 建立一個(gè)
vector<int> schAdm[maxm];
記錄每個(gè)學(xué)校錄取的學(xué)生合蔽。 - 設(shè)計(jì)一個(gè)排序函數(shù)击敌,將學(xué)生按分?jǐn)?shù)從高到低排序。
bool cmp(student s1, student s2) {
if((s1.ge + s1.gi) != (s2.ge + s2.gi)) {
return (s1.ge + s1.gi) > (s2.ge + s2.gi);
}
else
return s1.ge > s2.ge;
}
- 排序后拴事,遍歷結(jié)構(gòu)體數(shù)組谅猾,將學(xué)生添加到對(duì)應(yīng)學(xué)校中(如果沒(méi)有滿額)沼溜,如果滿額了,則與該學(xué)校錄取的最后一名學(xué)生的成績(jī)進(jìn)行比較,如果成績(jī)完全一樣凳忙,則超額錄取。否則军洼,遍歷該學(xué)生之后的志愿锤窑。
注意點(diǎn)
- 如何設(shè)計(jì)滿額后也可以判斷是否繼續(xù)錄取學(xué)生的功能。
// 當(dāng)學(xué)校滿員后調(diào)用該函數(shù),判斷時(shí)候可以超額錄取十厢。
bool isExceeded(int stuid, int schid) {
int last_stu_id = schAdm[schid][schQuota[schid]-1]; // 獲取學(xué)校的學(xué)生中最后一名的索引
if((Stu[last_stu_id].ge == Stu[stuid].ge) && (Stu[last_stu_id].gi == Stu[stuid].gi))
return true;
else return false;
}
AC代碼
#include <iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 40001;
const int maxm = 101;
const int maxk = 5;
struct student
{
int ge;
int gi;
int choices[maxk];
int key;
} Stu[maxn];
// 排序函數(shù)
bool cmp(student s1, student s2) {
if((s1.ge + s1.gi) != (s2.ge + s2.gi)) {
return (s1.ge + s1.gi) > (s2.ge + s2.gi);
}
else
return s1.ge > s2.ge;
}
int schQuota[maxm]; // 每個(gè)學(xué)校接受學(xué)生的名額上限
vector<int> schAdm[maxm]; // 記錄每個(gè)學(xué)校錄取的學(xué)生
// 判斷是否可以超限添加(當(dāng)兩者的分?jǐn)?shù)完全一樣等太,學(xué)校必須接收,無(wú)論名額是否超限)
bool isExceeded(int stuid, int schid) {
int last_stu_id = schAdm[schid][schQuota[schid]-1];
if((Stu[last_stu_id].ge == Stu[stuid].ge) && (Stu[last_stu_id].gi == Stu[stuid].gi))
return true;
else return false;
}
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < m; ++i)
{
scanf("%d", &schQuota[i]);
}
for (int i = 0; i < n; ++i)
{
int ge, gi;
scanf("%d%d", &ge, &gi);
Stu[i].ge = ge;
Stu[i].gi = gi;
Stu[i].key = i;
for(int j = 0; j < k; j++) {
int choice;
scanf("%d", &choice);
Stu[i].choices[j] = choice;
}
}
sort(Stu, Stu+n, cmp);
for (int i = 0; i < n; ++i)
{
int* choices;
choices = Stu[i].choices;
for (int j = 0; j < k; ++j)
{
//printf("schAdm[%d]: %d %d\n", choices[j], schAdm[choices[j]].size(), schQuota[choices[j]]);
if(schAdm[choices[j]].size() < schQuota[choices[j]]) {
//int stuid = Stu[i].key;
schAdm[choices[j]].push_back(i); // 記錄的是排序后學(xué)生所在數(shù)組的序號(hào)蛮放,并不是學(xué)生真正的id
break;
}
else {
if(isExceeded(i, choices[j])) {
schAdm[choices[j]].push_back(i);
break;
}
}
}
}
for (int i = 0; i < m; ++i)
{
vector<int> adm = schAdm[i]; // 因?yàn)橛涗浀氖桥判蚝笤跀?shù)組中的序號(hào)缩抡,這里獲取原來(lái)的id
vector<int> ids;
for (int j = 0; j < adm.size(); ++j)
{
int id = Stu[adm[j]].key;
ids.push_back(id);
}
sort(ids.begin(), ids.end());
for (int j = 0; j < ids.size(); ++j)
{
printf("%d", ids[j]);
if(j != ids.size()-1) printf(" ");
}
printf("\n");
}
}