九度テーマ1005:Graduate Admission


およびPAT 1080.Graduate Admission(平行志望シミュレーション問題)は同じです.学生の総得点が高いランキングが上位にあります.総得点は同じで、GE得点の高いランキングが上位にランクインした.GEが同じであれば,順位は同じである.
違うのは、9度で「Each input file may contain more than one test case.」
したがって、PAT 1080のコードを借りてmainにループを加え、ループ開始にinit()関数初期化変数を呼び出す.
九度はC++11をサポートしていないようです.autoを使用すると「warning:‘auto’will change meaning in C++0 x;please remove it」を与えました.
コード:
#include <cstdio>
#include <vector>
#include <algorithm>
#include <list>
#include <iostream>

using namespace std;

struct Applicant
{
	int m_number;
	int m_ge;
	int m_gi;
	list<int> m_choice;
	friend bool operator== (const Applicant& a, const Applicant& b)
	{
		return a.m_ge == b.m_ge && a.m_gi == b.m_gi;
	}
	friend bool operator< (const Applicant& a, const Applicant& b)
	{
		int aa = a.m_ge + a.m_gi;
		int bb = b.m_ge + b.m_gi;
		if (aa != bb)
		{
			return aa > bb;
		} else if (a.m_ge != b.m_ge)
		{
			return a.m_ge > b.m_ge; 
		} else
		{
			return a.m_number < b.m_number; ////// true
		}
	}
};

Applicant applicant[40010];
vector<int> school[110];
int n, m, k, quota[110];
int capacity, ge, gi, choice;

void choose_school(int begin, int end)
{
	bool full[110];
	for (int i = 0; i < m; ++ i)
	{
		full[i] = school[i].size() >= quota[i];
	}
	for (int i = begin; i < end; ++ i)
	{
		for (list<int>::iterator it = applicant[i].m_choice.begin(); it != applicant[i].m_choice.end(); ++ it)
		{
			if (full[*it] == false)
			{
				school[*it].push_back( applicant[i].m_number );
				break;
			}
		}
	}
}

void init()
{
	for (int i = 0; i < n; ++ i)
	{
		applicant[i].m_choice.clear();
	}
	for (int i = 0; i < m; ++ i)
	{
		school[i].clear();
	}
}

int main()
{
	while (cin >> n >> m >> k)
	{
		init();
		for (int i = 0; i < m; ++ i)
		{
			scanf("%d", quota + i);
		}
		for (int i = 0; i < n; ++ i)
		{
			applicant[i].m_number = i;
			scanf("%d%d", &applicant[i].m_ge, &applicant[i].m_gi);
			for (int j = 0; j < k; ++ j)
			{
				scanf("%d", &choice);
				applicant[i].m_choice.push_back(choice);
			}
		}
		sort(applicant, applicant+n);

		for (int i = 0; i < n; )
		{
			int begin = i;
			++ i;
			while (i < n && applicant[i] == applicant[begin])
			{
				++ i;
			}
			choose_school(begin, i);
		}

		for (int i = 0; i < m; ++ i)
		{
			sort (school[i].begin(), school[i].end());
			bool first = true;
			for (size_t j = 0; j < school[i].size(); ++ j)
			{
				if (first)
				{
					printf("%d", school[i][j]);
					first = false;
				} else
				{
					printf(" %d", school[i][j]);
				}
			}
			printf("
"); } } return 0; }