九度 题目1005:Graduate Admission

PAT 1080. Graduate Admission (平行志愿模拟题)是一样的:学生总分高的排名在前;总分相同,GE得分高的排名在前;GE相同,则排名相同。

不同的是,在九度中“Each input file may contain more than one test case.”

所以借用PAT 1080中的代码,在main中加入循环,在循环开始调用init()函数初始化变量。


注意到九度好像不支持C++11 —— 我使用auto时给出了"warning: ‘auto’ will change meaning in C++0x; 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("\n");
		}	
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值