PAT (Advanced) 1080. Graduate Admission (30)

如果我没有记错,这个题目曾经是浙大2011年研究生复试最后一题,当年的题目都比较水。上次刷完,这次又给小伙伴们来刷。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_N = 40000;
const int MAX_M = 100;

int N, M, K;

struct Student
{
	int id;
	int ge, gi, total;
	int choices[5];
	int rank;
}student[MAX_N];

bool cmp(const Student &a, const Student &b)
{
	if (a.total != b.total)
		return a.total > b.total;
	return a.ge > b.ge;
}

int quota[MAX_M];
int last_rank[MAX_M];
vector<int> adm_res[MAX_M];

int main()
{
	freopen("in.txt", "r", stdin);
	cin >> N >> M >> K;
	for (int i = 0; i < M; i++)
		cin >> quota[i];
	for (int i = 0; i < N; i++)
	{
		student[i].id = i;
		cin >> student[i].ge >> student[i].gi;
		student[i].total = student[i].ge + student[i].gi;
		for (int j = 0; j < K; j++)
			cin >> student[i].choices[j];
	}
	sort(student, student + N, cmp);
	student[0].rank = 0;
	for (int i = 1; i < N; i++)
	{
		if (student[i].total == student[i - 1].total && student[i].ge == student[i - 1].ge)
			student[i].rank = student[i - 1].rank;
		else
			student[i].rank = i;
	}
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < K; j++)
		{
			int sch_id = student[i].choices[j];
			if (adm_res[sch_id].size() < quota[sch_id])
			{
				adm_res[sch_id].push_back(student[i].id);
				last_rank[sch_id] = student[i].rank;
				break;
			}
			else if (!adm_res[sch_id].empty() && student[i].rank == last_rank[sch_id])
			{
				adm_res[sch_id].push_back(student[i].id);
				break;
			}
		}
	}
	for (int i = 0; i < M; i++)
	{
		if (adm_res[i].empty())
			cout << endl;
		else
		{
			sort(adm_res[i].begin(), adm_res[i].end());
			cout << adm_res[i][0];
			for (int j = 1; j < adm_res[i].size(); j++)
				cout << " " << adm_res[i][j];
			cout << endl;
		}
	}
}


你可能感兴趣的:(PAT (Advanced) 1080. Graduate Admission (30))