略微麻烦了点。主要是一开始题意理解错误了。
Debug记录:
1.忘了
2.当之前的第i轮志愿扫描录取时有滞留考生在队首,后面某一轮又录取时,不能采取pop_front 操作,否则删除的是队首滞留考生
改进方法:不采用录取删除的做法,而是在Graduate结构体内部设立了一个admitSch标志来做标记。
3.
if (grad[j].isAdmit=true){//判等符号又写成了赋值符号
continue;
}
4.算法错误:
理解有误:并不是梯度志愿,而是平行志愿! 完全按照考生排名决定优先选择权:第i名依照志愿顺序确定完学校后,才能轮到第i+1名
5.由于对Graduate的排序有两种:一种是按分数,一种是按编号,故应该编写两个cmp来实现,不要重载运算符
-
题目描述:
-
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
-
答疑:
- 解题遇到问题?分享解题心得?讨论本题请访问: http://t.jobdu.com/thread-7729-1-1.html
#include <cstdio>
#include <vector>
#include <algorithm>
#define KMAX 5
using namespace std;
struct Graduate;
struct School{
int num;
vector <Graduate> admitList;
int quota;
School(int num,int quota){
this->num=num;
this->quota=quota;
}
};
struct Graduate{
int num;
int admitSch;
int GE,GI;
int G;
int preferList[KMAX];
Graduate(int num,int GE,int GI){
admitSch=-1;
this->num=num;
this->GE=GE;
this->GI=GI;
this->G=GE+GI;
}
static bool cmp_grade(const Graduate &g1,const Graduate &g2){//升序排列
bool cmp;
if (g1.G>g2.G)
cmp=true;
else if (g1.G<g2.G)
cmp=false;
else{
if (g1.GE>g2.GE)
cmp=true;
else//rank same
cmp=false;
}
return cmp;
}
static bool cmp_index(const Graduate &g1,const Graduate &g2){//升序排列
return g1.num<g2.num;
}
bool operator==(const Graduate &g)const{
if (GE==g.GE&&GI==g.GI)
return true;
else
return false;
}
};
int main(){
int n,m,k;
vector<School> sch;
vector<Graduate> grad;
int quota,GE,GI,pref;
bool firstCase;
while (scanf("%d%d%d",&n,&m,&k)!=EOF){
//initiate
sch.clear();
grad.clear();
//input
for (int i=0;i<m;i++){//school
scanf("%d","a);
sch.push_back(School(i,quota));
}
for (int i=0;i<n;i++){//graduate
scanf("%d%d",&GE,&GI);
grad.push_back(Graduate(i,GE,GI));
for (int j=0;j<k;j++){
scanf("%d",&pref);
grad.back().preferList[j]=pref;
}
}
//sort
sort(grad.begin(),grad.end(),Graduate::cmp_grade);//降序排列
//process
for (int j=0;j<grad.size();j++){//对grad队列中剩下的考生进行遍历 ,当前遍历为grad[j]
for (int i=0;i<k;i++){//该考生按照报考志愿逐一往下选择学校录取
School &tgtSch=sch[grad[j].preferList[i]];/*这个考生的第i志愿的学校*/
if ( (tgtSch.admitList.size()<tgtSch.quota) || (grad[j]==tgtSch.admitList.back()) ){
tgtSch.admitList.push_back(grad[j]);//当前考生录取 ,
grad[j].admitSch=tgtSch.num;//做标记
break;
}
}
}
//adjust
for (int i=0;i<sch.size();i++){
sort(sch[i].admitList.begin(),sch[i].admitList.end(),Graduate::cmp_index);
}
//output
for (int i=0;i<m;i++){
firstCase=true;
for (int j=0;j<sch[i].admitList.size();j++){
if (firstCase)
firstCase=false;
else
printf(" ");
printf("%d",sch[i].admitList[j].num);
}
printf("\n");
}
}
return true;
}