PAT甲级1025 PAT Ranking (25分)

#pragma warning(disable:4996)
#include
#include
#include
#include
using namespace std;

struct information
{
     
	char id[15];
	int location_number;
	int score;
	int local_rank;
	int final_rank;
}info[30010];

bool cmp(struct information a, struct information b)
{
     
	if (a.score != b.score)
		return a.score > b.score;
	else
	{
     
		int s = strcmp(a.id, b.id);
		return s < 0;
	}
}

int main()
{
     
	int N;
	int count = 0;
	scanf("%d", &N);
	for (int i = 0; i < N; i++)
	{
     
		int K;
		scanf("%d", &K);
		for (int j = 0; j < K; j++)
		{
     
			scanf("%s%d", info[count].id, &info[count].score);
			info[count].location_number = i + 1;
			count++;
		}
		sort(info + count - K, info + count, cmp);

		info[count - K].local_rank = 1;
		for (int j = count - K + 1; j < count; j++)
		{
     
			if (info[j].score == info[j - 1].score)
			{
     
				info[j].local_rank = info[j - 1].local_rank;
			}
			else
			{
     
				info[j].local_rank = j + 2 - (count - K + 1);
			}

		}
	}
	printf("%d\n", count);

	sort(info, info + count, cmp);

	info[0].final_rank = 1;
	printf("%s %d %d %d\n", info[0].id, info[0].final_rank, info[0].location_number, info[0].local_rank);

	for (int i = 1; i < count; i++)
	{
     
		if (info[i - 1].score == info[i].score)
			info[i].final_rank = info[i - 1].final_rank;
		else
			info[i].final_rank = i + 1;
		printf("%s %d %d %d\n", info[i].id, info[i].final_rank, info[i].location_number, info[i].local_rank);
	}
	return 0;
}

你可能感兴趣的:(PAT)