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 4Sample Output:
0 10 3 5 6 7 2 8 1 4
这个题我做26分,差两个测试点,还不知道是哪做错了···。
注意排名和上一个人相同的可以破格录取。
输出时候按照原来id输出。
我的代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct applicants{
int ge,gi,final,rank,id;
int zhiyuan[10];
}app[40010];
struct school{
int minge,now;
int member[400010];
}sch[110];
bool cmp(applicants a,applicants b)
{
if(a.final!=b.final) return a.final>b.final;
else return a.ge>b.ge;
}
bool cmp1(int a,int b)
{
return a<b;
}
int main()
{
#ifndef ONLINEJUDGE
freopen("data.in","r",stdin);
#endif
int n,m,k,i,j;
scanf("%d%d%d",&n,&m,&k);
for(i=0;i<m;i++)
{
scanf("%d",&sch[i].minge);
sch[i].now=0;
}
for(i=0;i<n;i++)
{
scanf("%d%d",&app[i].ge,&app[i].gi);
app[i].final=app[i].ge+app[i].gi;
app[i].id=i;
for(j=0;j<k;j++)
scanf("%d",&app[i].zhiyuan[j]);
}
sort(app,app+n,cmp);
app[0].rank=1;
for(i=0;i<n;i++)
{
if(app[i].final==app[i-1].final&&app[i].ge==app[i-1].ge)
app[i].rank=app[i-1].rank;
else app[i].rank=i+1;
}
int temp=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
temp=app[i].zhiyuan[j];
if(sch[temp].now<sch[temp].minge||(app[sch[temp].member[sch[temp].now-1]].rank==app[i].rank))
{
sch[temp].member[sch[temp].now]=app[i].id;
sch[temp].now++;
break;
}
}
}
for(i=0;i<m;i++)
{
if(sch[i].now==0||sch[i].minge==0)
printf("\n");
else{
sort(sch[i].member,sch[i].member+sch[i].now,cmp1);
for(j=0;j<sch[i].now;j++)
{
printf("%d",sch[i].member[j]);
if(j==sch[i].now-1)
printf("\n");
else printf(" ");
}
}
}
return 0;
}
满分的代码:
#include <iostream> #include <vector> #include <algorithm> #include <bitset> using namespace std; struct Applicant { int id; int EGrade; int IGrade; double final; int choice[6]; int rank; }; Applicant applicants[40000]; bool cmp(const Applicant& lhs, const Applicant& rhs) { if (lhs.final == rhs.final) return lhs.EGrade > rhs.EGrade; return lhs.final > rhs.final; } int main() { int applicantNum, schoolNum, choiceNum; cin >> applicantNum >> schoolNum >> choiceNum; int quota[100]; for (int i = 0; i < schoolNum; i++) cin >> quota[i]; for (int i = 0; i < applicantNum; i++) { applicants[i].id = i; cin >> applicants[i].EGrade >> applicants[i].IGrade; applicants[i].final = (applicants[i].EGrade + applicants[i].IGrade) / 2.0; for (int j = 0; j < choiceNum; j++) cin >> applicants[i].choice[j]; } sort(applicants, applicants + applicantNum, cmp); //set the rank of the all applicants int final=0, EGrade=0, rank = 1; for (int i = 0; i < applicantNum; i++) { if (applicants[i].final == final&&applicants[i].EGrade == EGrade) applicants[i].rank = rank; else { final = applicants[i].final; EGrade = applicants[i].EGrade; rank = i + 1; applicants[i].rank = rank; } } bitset<100000> empty; vector<int> vec[100]; for (int i = 0; i < schoolNum; i++) if (quota[i] <= 0) empty.set(i); int i = 0; rank = 1; while (1) { //admit the applicants with same rank while (i < applicantNum&&applicants[i].rank == rank) { for (int j = 0; j < choiceNum; j++) { if (!empty[applicants[i].choice[j]]) { quota[applicants[i].choice[j]]--; vec[applicants[i].choice[j]].push_back(applicants[i].id); break; } } i++; } if (i == applicantNum) break; else rank = applicants[i].rank; //next rank for (int j = 0; j < schoolNum; j++) if (quota[j] <= 0) empty.set(j); } for (int i = 0; i < schoolNum; i++) { sort(vec[i].begin(), vec[i].end()); for (int j = 0; j < vec[i].size(); j++) { cout << vec[i][j]; if (j < vec[i].size() - 1) cout << " "; } cout << endl; } }