题解 | #Clau,红豆与弗老板打多校#

Clau,红豆与弗老板打多校

https://ac.nowcoder.com/acm/contest/121107/E

E

数据结构

  • 使用 vector<pair<int,int>> 存储选手数据
  • 每个 pair<int,int> 表示一个选手的 (主要分数, 次要分数)

排序规则

sort(a.begin(), a.end(), [](auto &p1, auto &p2){
    if (p1.first != p2.first) return p1.first > p2.first; 
    return p1.second < p2.second; 
});

排序优先级:

  1. 主要分数降序:first 大的排在前面
  2. 次要分数升序:如果主要分数相同,second 小的排在前面

根据排名百分比划分奖牌等级: gold前 10% (n / 10) silver前 30% (n * 3 / 10) bronze前 60% (n * 6 / 10) iron后 40%

查找特定选手排名

  • self = a[0] 保存了要查询的选手(第一个输入的选手)
  • 在排序后的数组中查找该选手的位置,计算其排名
#include <bits/stdc++.h>
using namespace std;

int main() {
    int n; 
    cin >> n;
    vector<pair<int,int>> a(n);
    for (int i=0;i<n;i++) cin >> a[i].first >> a[i].second;
    auto self = a[0];
    
    sort(a.begin(), a.end(), [](auto &p1, auto &p2){
        if (p1.first != p2.first) return p1.first > p2.first; 
        return p1.second < p2.second; 
    });
    
    int rank = 0;
    for (int i=0;i<n;i++){
        if (a[i] == self){
            rank = i + 1;
            break;
        }
    }
    
    int g = n / 10;
    int s = n * 3 / 10;
    int b = n * 6 / 10;

    if (rank <= g) cout << "gold\n";
    else if (rank <= s) cout << "silver\n";
    else if (rank <= b) cout << "bronze\n";
    else cout << "iron\n";
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务