PAT (Advanced) 1025. PAT Ranking (25)

水题一个,但是因为排序规则看得不够仔细,有个6分测试点始终通不过,耗费了大量时间,悲剧哭。以下完整AC代码:

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

using namespace std;

struct Testee
{
	string id;
	int score;
	int location_num;
	int loca_rank;
	int final_rank;
};

vector<Testee> testee_final;
vector<Testee> testee_local;

bool cmp(const Testee &a, const Testee &b)
{
	if (a.score != b.score)
		return a.score > b.score;
	else
		return a.id < b.id;
}

int main()
{
	int n;
	cin >> n;
	int tot = 0;
	for (int i = 0; i < n; i++)
	{
		int k;
		cin >> k;
		tot += k;
		Testee tmp;
		testee_local.clear();
		for (int j = 0; j < k; j++)
		{
			cin >> tmp.id >> tmp.score;
			tmp.location_num = i;
			testee_local.push_back(tmp);
		}
		sort(testee_local.begin(), testee_local.end(), cmp);
		testee_local[0].loca_rank = 0;
		for (int i = 0; i < k - 1; i++)
		{
			if (testee_local[i + 1].score == testee_local[i].score)
				testee_local[i + 1].loca_rank = testee_local[i].loca_rank;
			else
				testee_local[i + 1].loca_rank = i + 1;
		}
		for (int j = 0; j < k; j++)
		{
			tmp = testee_local[j];
			testee_final.push_back(tmp);
		}
	}
	sort(testee_final.begin(), testee_final.end(), cmp);
	testee_final[0].final_rank = 0;
	for (int i = 0; i < tot - 1; i++)
	{
		if (testee_final[i + 1].score == testee_final[i].score)
			testee_final[i + 1].final_rank = testee_final[i].final_rank;
		else
			testee_final[i + 1].final_rank = i + 1;
	}
	cout << tot << endl;
	for (int i = 0; i < tot; i++)
	{
		cout << testee_final[i].id << " " << testee_final[i].final_rank + 1 << " " << testee_final[i].location_num + 1 << " " << testee_final[i].loca_rank + 1 << endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值