题解:AT_pakencamp_2025_day2_a A Strange Ranking Problem

· · 题解

Atcoder 原题传送门

本题涉及结构体排序内容,建议先完成此题。

思路

由题,观察公式可得,对于一位选手,其第 i 天的分数 s_i,则他的总分为

10^{100} \times (s_1 \times 1 + s_2 \times 2 + s_3 \times 4 + s_4 \times 8) - i

由于所有选手的分数均被乘上了 10^{100},所以可以忽略不计,我们只需要按照选手的分数从大到小排序再输出前四位就可以了。

注意到选手的总分可能完全一样,所以我们需要记录每位选手的序号,当两位选手的总分相同时,则按照选手编号从小到大进行排序。

代码

#include <bits/stdc++.h>
using namespace std;
int n, m;

struct node
{
    int id, score;
}stu[35]; // 结构体存储选手编号及分数

bool cmp(node x, node y) // 自定义排序规则
{
    if (x.score != y.score)
    {
        return x.score > y.score;
    } // 按照分数降序排序
    return x.id < y.id; // 若分数相同,则按照编号升序排序
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        int a, b, c, d;
        cin >> a >> b >> c >> d; // 输入四天的分数
        stu[i].score = a + b * 2 + c * 4 + d * 8; // 按照题中所给的公式计算总分
        stu[i].id = i; // 记录选手编号
    }
    sort(stu + 1, stu + 1 + n, cmp); // 进行排序
    for (int i = 1; i <= 4; ++i)
    {
        cout << stu[i].id << "\n";
    } // 输出前四位选手的序号
    return 0;
}
// Code by 482.