PAT甲級 1025 PAT Ranking (25分)

排序的一道題,比較複雜的是地區排名,我用了一個二維數組來解決

#include<bits/stdc++.h>
using namespace std;
struct test{
	long long number;
	int final_rank;
	int location_number;
	int local_rank;
	int score;
};
bool cmp(struct test a,struct test b)
{
	if(a.score!=b.score)
	return a.score>b.score;
	else
	return a.number < b.number;
}
int local[105][3];
main()
{
	int N;
	for(int i=0;i<105;i++)
	{
		local[i][0] = 0; //當前排名成績 
		local[i][1] = 1; //當前排名 
		local[i][2] = 0; //當前排名人數 
	}
	struct test testes[30000];
	int cnt = 0;
	scanf("%d",&N);
	for(int i=0;i<N;i++)
	{
		int k;
		scanf("%d",&k);
		for(int j=0;j<k;j++)
		{
			cin>>testes[cnt].number>>testes[cnt].score;
			testes[cnt].location_number = i+1;
			if(testes[cnt].score>local[testes[cnt].location_number][0])
			local[testes[cnt].location_number][0] = testes[cnt].score; //更新當前地區最高分 
			cnt++;
		}
	}
	sort(testes,testes+cnt,cmp);
	for(int i=0;i<cnt;i++)
	{
		if(testes[i].score != testes[i-1].score)
		{
			testes[i].final_rank = i+1;
		}
		else
		{
			testes[i].final_rank = testes[i-1].final_rank;
		}
		if(testes[i].score != local[testes[i].location_number][0])
		{
			testes[i].local_rank = local[testes[i].location_number][1]+ local[testes[i].location_number][2]; //地區排名 = 上一排名+上一排名人數 
			local[testes[i].location_number][0] = testes[i].score; //更新當前分數 
			local[testes[i].location_number][1] = testes[i].local_rank; //更新當前排名 
			local[testes[i].location_number][2] = 1;	 //更新當前排名人數 
		}
		else
		{
				testes[i].local_rank = local[testes[i].location_number][1] ; //對應排名即當前排名 
				local[testes[i].location_number][2] +=1; //當前排名人數+1 
		}
	}
	printf("%d\n",cnt);
	for(int i=0;i<cnt;i++)
	{
		printf("%lld %d %d %d\n",testes[i].number,testes[i].final_rank,testes[i].location_number,testes[i].local_rank);
	}
}
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章