PAT (A) 1080 Graduate Admission (full score 30)

Title translation:
It is said that in 2011, about 100 graduate schools in Zhejiang Province are preparing to process more than 40,000 applications. If you can write a program to automate the admission process, that would be very helpful.
Each applicant must provide two levels: the national entrance examination level Ge and the interview level Gi. The applicant's final grade is (Ge + Gi)/2. The admission rules are:
(1). Applicants will be ranked according to their final results, and will be admitted from the highest to the lowest in the ranking list.
(2). If the final scores are tied, the applicants will be ranked according to their national entrance examination grade Ge. If they are still tied, their rankings must be the same.
(3). Each applicant may have K choices, and will be admitted according to his/her choice: if according to the ranking list, it is time to admit; if it does not exceed the quota of one's favorite school, then The person will be admitted to this school, otherwise one's other options will be considered one by one in order. If a person is rejected by all preferred schools, then the unfortunate applicant will be rejected.
(4). If the rankings are tied and the corresponding applicants are applying at the same school, the school must admit all applicants with the same grade even if the quota is exceeded.

Input requirements:
Each input file contains a 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 applicants can choose.
On the next line, separated by spaces, there are M positive integers. The i-th integer is the quota of the i-th graduate school.
Then, N lines follow, each line contains 2 + K integers, separated by spaces. The first two integers are Ge and Gi of the applicant respectively. The next K integers represent the preferred schools. For the sake of simplicity, we assume that the number of the school ranges from 0 to M-1, and the number of applicants ranges from 0 to N-1.

Output requirements:
For each test case, you should output the admission results of all graduate schools. The grades of each school must occupy one line, which contains the number of applicants admitted by the school. The numbers must be arranged in ascending order and separated by spaces. There must be no extra spaces at the end of each line. If no students are admitted by the school, then blank lines must be output accordingly.

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

This question was not clear at the beginning of my own thinking, so I used the idea of ​​an UP master at station B for reference.

The following code was written by myself, but after running it only got one-third of the score. There are two test points with data errors, and the wrong problem is still looking for. First post this code:

#include<iostream>
#include<algorithm>
#include <vector>

using namespace std;

struct student {
    
    
	int id;
	int ge;
	int gi;
	int g3;
	int b[5];
};

bool cmp(student a, student b) {
    
    
	if (a.g3 != b.g3) 
		return a.g3 > b.g3;
//	else
		return a.ge > b.ge;
}

bool cmpId(student a, student b) {
    
    
	return a.id < b.id;
}

int main()
{
    
    
	struct student *stu;
	int n, m, k;
	// n申请人总数,m研究生院总数,k申请人可以选择的数量
	cin >> n >> m >> k;
	vector<student> inschool[100];
	int a[101] = {
    
     0 }; // 每个研究生院的配额
	for (int i = 0; i < m; i++)
	{
    
    
		int g;
		cin >> g;
		a[i] = g;
	}
	//为结构体分配存储空间
	stu = (struct student *)malloc(40000* sizeof(struct student));
	for (int i = 0; i < n; i++) // 为所有学生输入他的各项信息
	{
    
    
		stu[i].id = i;
		int c, d, e;
		cin >> c >> d;
		stu[i].ge = c;
		stu[i].gi = d;
		stu[i].g3 = (c + d) / 2;
		for (int j = 0; j < k; j++)
		{
    
    
			cin >> e;
			stu[i].b[j] = e;
		}
	}

	sort(stu, stu+n-1, cmp);
	for (int i = 0; i < n; i++) {
    
    
		for (int j = 0; j < k; j++) {
    
    
			int want = stu[i].b[j];
			if (inschool[want].size() < a[want]) {
    
     
				inschool[want].push_back(stu[i]);
				break;
			}
			else {
    
    
				if (inschool[want].back().g3 == stu[i].g3 &&
					inschool[want].back().ge == stu[i].ge) {
    
    
					inschool[want].push_back(stu[i]);
					break;
				}
			}
		}
	}

	for (int i = 0; i < m; i++) {
    
    
		if (inschool[i].size() == 0)
			cout << endl;
		else {
    
    
			sort(inschool[i].begin(), inschool[i].end(), cmpId);
			for (int j = 0; j < inschool[i].size(); j++) {
    
    
				if (j)
					cout << " ";
				cout << inschool[i][j].id;
			}
			cout << endl;
		}
	}

	system("pause");
	return 0;
}

Full score code: from a B station UP, also posted it to continue learning.
The B station information
Insert picture description here
code of this UP is as follows:

#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

struct stu {
    
    
    int id;
    double g1, g2, g3;
    int choice[5];
};

bool cmp(stu a, stu b) {
    
    
    if (a.g3 != b.g3) return a.g3 > b.g3;
    return a.g1 > b.g1;
}

bool cmpId(stu a, stu b) {
    
    
    return a.id < b.id;
}

int main() {
    
    
    int n, m, k;
	scanf("%d %d %d", &n, &m, &k);
    int schoolsLimit[100];
	vector<stu> inschool[100];
    vector<stu> students; 
	stu tmp;
    for (int i = 0; i < m; ++i) 
	scanf("%d", &schoolsLimit[i]);
    for (int i = 0; i < n; ++i) {
    
    
        tmp.id = i;
        scanf("%lf %lf", &tmp.g1, &tmp.g2);
        tmp.g3 = (tmp.g1 + tmp.g2) / 2;
        for (int j = 0; j < k; ++j) 
		scanf("%d", &tmp.choice[j]);
        students.push_back(tmp);
    }
    
    sort(students.begin(), students.end(), cmp); // rank靠前的先安排
    for (int i = 0; i < n; ++i) {
    
    
        for (int j = 0; j < k; ++j) {
    
    
            int want = students[i].choice[j];
            if (inschool[want].size() < schoolsLimit[want]) {
    
     // 想去的学校还有名额
                inschool[want].push_back(students[i]);
				break;
            }
            else {
    
    
                if (inschool[want].back().g3 == students[i].g3 && 
				inschool[want].back().g1 == students[i].g1) {
    
     
				// 没名额了 但是该考生和该校录取的最后一名高考总分相同
                    inschool[want].push_back(students[i]);
					break;
                }
            }
        }
    }
    for (int i = 0; i < m; i++) {
    
    
        if (inschool[i].size() == 0) 
		printf("\n");
        else {
    
    
            sort(inschool[i].begin(), inschool[i].end(), cmpId);
            for (int j = 0; j < inschool[i].size(); j++) {
    
    
                if (j) 
				printf(" ");
                printf("%d", inschool[i][j].id);
            }
            printf("\n");
        }
    }
    return 0;
}

Postscript: My abilities are still limited, so I should lay a solid foundation first. After doing a lot of PAT Level B questions, I will come back to PAT Level A questions.
It's time to sleep, starting tomorrow, let's start with simple questions!

Guess you like

Origin blog.csdn.net/qq_27538633/article/details/105721957