CF24B F1 Champions 题解

Content

\(n\) 场已经进行完的赛车比赛,每场比赛给出前 \(m\) 名的名字。在每场比赛中,前 \(10\) 名的选手分别可以获得 \(25,18,15,12,10,8,6,4,2,1\) 分,其他名次的选手不得分。现在给出两种排序方式:

  1. 先按照得分降序排序,如果得分相同,按照得到第一名的次数降序排序,如果还是相同,就按照得到第二名的次数降序排序,以此类推,直到比较出来为止。
  2. 先按照得到第一名的次数降序排序,如果次数相同,按照得分降序排序,如果还是相同,就按照得到第二名的次数降序排序,以此类推,直到比较出来为止。

请求出按照两种排序方式排序之后分别得出来的第一名。

数据范围:\(1\leqslant n\leqslant 20,1\leqslant m\leqslant 50\)

Solution

结构体排序好题。

我们开个结构体,把每个人的名字、得分以及得到的名次的次数情况储存下来:

struct player {
	string name;
	int score, ranking[57];
}a[1007], b[1007];

然后就是如何通过输入储存了,我们还是利用 \(\texttt{map}\) 将每个人的名字映射到其编号上,每出现一个新的名字,就新开一个空间来存储,并统计这个人的分数和排名情况。

那么为什么要开两组结构体呢?这样好方便排序,所以我们再复制一组相同的结构体,然后按照上面的两个排序方式排序即可,可以写两个 \(\texttt{cmp}\) 函数,然后直接按照上面的方式模拟即可。

Code

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <map>
using namespace std;

const int getscore[51] = {0, 25, 18, 15, 12, 10, 8, 6, 4, 2, 1};
map<string, int> vis;
int n, cnt;
string names;
struct player {
	string name;
	int score, ranking[57];
}a[1007], b[1007];

bool cmp1(const player& p1, const player& p2) {
	if(p1.score != p2.score) return p1.score > p2.score;
	else
		for(int i = 1; i <= 50; ++i)
			if(p1.ranking[i] != p2.ranking[i]) return p1.ranking[i] > p2.ranking[i];
}
bool cmp2(const player& p1, const player& p2) {
	if(p1.ranking[1] != p2.ranking[1]) return p1.ranking[1] > p2.ranking[1];
	else if(p1.score != p2.score) return p1.score > p2.score;
	else
		for(int i = 2; i <= 50; ++i)
			if(p1.ranking[i] != p2.ranking[i]) return p1.ranking[i] > p2.ranking[i];
}

int main() {
	scanf("%d", &n);
	while(n--) {
		int m;
		scanf("%d", &m);
		for(int i = 1; i <= n; ++i) {
			cin >> names;
			if(!vis[names]) {
				vis[names] = ++cnt;
				a[vis[names]].name = names;
			}
			a[vis[names]].score += getscore[i];
			a[vis[names]].ranking[i]++;
		}
	}
	for(int i = 1; i <= cnt; ++i) b[i] = a[i];
	sort(a + 1, a + cnt + 1, cmp1);
	sort(b + 1, b + cnt + 1, cmp2);
	cout << a[1].name << endl << b[1].name;
	return 0;
}
posted @ 2021-12-16 15:12  Eason_AC  阅读(40)  评论(0)    收藏  举报