PAT (Advanced) 1012. The Best Rank (25)

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

using namespace std;

struct Student
{
	string id;
	int c, m, e, a;
	int rank[4];// a, c, m, e
};
vector<Student> student;
bool cmp_c(const Student &a, const Student &b)
{
	return a.c > b.c;
}

bool cmp_m(const Student &a, const Student &b)
{
	return a.m > b.m;
}

bool cmp_e(const Student &a, const Student &b)
{
	return a.e > b.e;
}

bool cmp_a(const Student &a, const Student &b)
{
	return a.a > b.a;
}

int main()
{
//	freopen("in.txt", "r", stdin);
	int N, M;
	cin >> N >> M;
	Student stu;
	for (int i = 0; i < N; i++)
	{
		cin >> stu.id >> stu.c >> stu.m >> stu.e;
		stu.a = stu.c + stu.m + stu.e;
		student.push_back(stu);
	}
	sort(student.begin(), student.end(), cmp_a);
	student[0].rank[0] = 0;
	for (int i = 0; i < N - 1; i++)
	{
		if (student[i + 1].a == student[i].a)
			student[i + 1].rank[0] = student[i].rank[0];
		else
			student[i + 1].rank[0] = i + 1;
	}
	sort(student.begin(), student.end(), cmp_c);
	student[0].rank[1] = 0;
	for (int i = 0; i < N - 1; i++)
	{
		if (student[i + 1].c == student[i].c)
			student[i + 1].rank[1] = student[i].rank[1];
		else
			student[i + 1].rank[1] = i + 1;
	}
	sort(student.begin(), student.end(), cmp_m);
	student[0].rank[2] = 0;
	for (int i = 0; i < N - 1; i++)
	{
		if (student[i + 1].m == student[i].m)
			student[i + 1].rank[2] = student[i].rank[2];
		else
			student[i + 1].rank[2] = i + 1;
	}
	sort(student.begin(), student.end(), cmp_e);
	student[0].rank[3] = 0;
	for (int i = 0; i < N - 1; i++)
	{
		if (student[i + 1].e == student[i].e)
			student[i + 1].rank[3] = student[i].rank[3];
		else
			student[i + 1].rank[3] = i + 1;
	}

	vector<string> toreq;
	string tmp;
	for (int i = 0; i < M; i++)
	{
		cin >> tmp;
		toreq.push_back(tmp);
	}
	int pos, min;
	for (int i = 0; i < M; i++)
	{
		bool flag = false;
		for (int j = 0; j < N; j++)
		{
			if (student[j].id == toreq[i])
			{
				flag = true;
				min = student[j].rank[0];
				pos = 0;
				for (int k = 1; k < 4; k++)
				{
					if (min > student[j].rank[k])
					{
						min = student[j].rank[k];
						pos = k;
					}
				}
				break;
			}
		}
		if (!flag)
			cout << "N/A" << endl;
		else
		{
			cout << min + 1 << " ";
			switch (pos)
			{
			case 0:
				cout << "A";
				break;
			case 1:
				cout << "C";
				break;
			case 2:
				cout << "M";
				break;
			case 3:
				cout << "E";
				break;
			}
			cout << endl;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值