1080 Graduate Admission (30 分)

1080 Graduate Admission (30 分)

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 4
Sample Output:
0 10
3
5 6 7
2 8

1 4

代码

ps:1.有两个测试点没有通过。。。
2.一个赋值语句的 = 写成了 == 导致查bug 查了半个小时。

#include
#include
#include
using namespace std;
struct stu {
  int rank;
  int sum,id;
  int want[5];
  int score[2];
}students[40000],origin[40000];

int cmp(stu a, stu b) {
  return a.sum == b.sum ? a.score[0] > b.score[0] : a.sum > b.sum;
}

int main() {
  int save[100][100] = {-1}; // 录取的学号统计
  int ac[100] = {0}; // 已经录取的人数
  int M[100]; // 各学校要的人数
  int num, Mnum, Knum;
  cin >> num >> Mnum >> Knum;
  int i,j;
  for(i = 0; i < Mnum; i++) cin >> M[i];
  for(i = 0; i< num; i++) {
    cin >> students[i].score[0] >> students[i].score[1];
    students[i].sum = students[i].score[0] + students[i].score[1];
    students[i].id = i;
    for(j = 0; j < Knum; j++) cin >> students[i].want[j];
    origin[i] = students[i];
  }
  sort(students, students + num, cmp);
 

  students[0].rank = 1;
  origin[students[0].id].rank = 1;
  for(i = 1; i < num; i++ ){
    if(students[i].score[0] == students[i - 1].score[0] && students[i].sum == students[i - 1].sum) {students[i].rank = students[i - 1].rank;  origin[students[i].id].rank = students[i - 1].rank;}
    else {students[i].rank = i + 1; origin[students[i].id ].rank = i + 1;}
	//cout << students[i].id
  } 
//  for( i = 0; i < num; i++){ cout << origin[i].id <<" "<< origin[i].rank<< endl;}//
  int flag[40000] ={0};
  for(i = 0; i < num; i++) {
    for(j = 0; j < Knum; j++ ){
      int temp = students[i].want[j];
      if(ac[temp] < M[temp] && flag[students[i].id] == 0) { save[temp][ac[temp]] = students[i].id; ac[temp]++; flag[students[i].id] =  1;}
      else if(ac[temp] == M[temp] && origin[save[temp][ac[temp]-1]].rank == students[i].rank && flag[students[i].id] == 0 && M[temp] != 0 ){ save[temp][ac[temp]] = students[i].id; ac[temp]++;  flag[students[i].id] =  1;}
      }
  }
  for(i = 0; i < Mnum; i++) sort(save[i], save[i] + ac[i]);
  for(i = 0; i < Mnum; i++) {
    if(ac[i] == 0){printf("\n"); continue;}
    for(j = 0; j < ac[i]; j++){ 
      if(j < ac[i] - 1) printf("%d ",save[i][j]);
      else printf("%d", save[i][j]);
    }
    printf("\n");
  }
  return 0;
}

提升

1.save可以写成结构体类型,保存更方便
2.先确定数组大小,再定义数组
3.不用为了tie而再去重新排名,只需记录最后录取同学的序号,通过save找到,判定其总成绩和ge都相等即可

改进的代码

#include 
#include 
#include 
using namespace std;
struct stu{
    int id, ge, gi, sum;
    vector want;
};
bool cmp(stu& a, stu& b) {
    if (a.sum != b.sum) return a.sum > b.sum;
    return a.ge > b.ge;
}
bool cmp2(stu& a, stu& b) {
  return a.id < b.id;
}
int main(){
    int n, m, k, ac[110], cnt[110] = {0};
    scanf("%d%d%d", &n, &m, &k);
    vector stu(n), save[110];
    for(int i = 0; i < m; i++)
        scanf("%d",&ac[i]);
    for(int i = 0; i < n; i++) {
        scanf("%d%d", &stu[i].ge, &stu[i].gi);
        stu[i].id = i;
        stu[i].sum = stu[i].ge + stu[i].gi;
        stu[i].want.resize(k);
        for(int j = 0; j < k; j++)
            scanf("%d", &stu[i].want[j]);
    }
    sort(stu.begin(), stu.end(), cmp);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < k; j++) {
            int temp = stu[i].want[j]; //想去的学校的序号
            int lastindex = cnt[temp] - 1;
            if(cnt[temp] < ac[temp] || (stu[i].sum == save[temp][lastindex].sum) && stu[i].ge == save[temp][lastindex].ge) {
                save[temp].push_back(stu[i]);
                cnt[temp]++;
                break;
            }
        }
    }
    for(int i = 0; i < m; i++) {
        sort(save[i].begin(), save[i].end(), cmp2);
        for(int j = 0; j < cnt[i]; j++) {
            if(j != 0) printf(" ");
            printf("%d", save[i][j].id);
        }
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(PAT-A)