P1786 帮贡排序

// Problem: P1786 帮贡排序
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1786
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// User: Pannnn

#include <bits/stdc++.h>

using namespace std;

struct People {
    string name;
    string position;
    int contribute;
    int grade;
    int id;
};

void adjust(vector<pair<string, int>> &posInfo, map<string, int> &posMap, vector<People> &info) {
    // 按照帮贡排序,如果帮贡一样,按输入顺序排列
    sort(info.begin(), info.end(), [](People p1, People p2) -> bool {
        if (p1.contribute != p2.contribute) {
            return p1.contribute > p2.contribute;
        } else {
            return p1.id < p2.id;
        }
    });
    
    int idx = 0;
    for (int i = 0; i < info.size(); ++i) {
        if (!info[i].position.empty()) {
            continue;
        }
        while (posInfo[idx].second == 0) { ++idx; }
        info[i].position = posInfo[idx].first;
        --posInfo[idx].second;
    }
    // 按现在职位排序,如职位相同,再按等级排序,如等级一样,按输入顺序排列
    sort(info.begin(), info.end(), [&posMap](People p1, People p2)-> bool {
        if (posMap[p1.position] != posMap[p2.position]) {
            return posMap[p1.position] < posMap[p2.position];
        } else if (p1.grade != p2.grade){
            return p1.grade > p2.grade;
        } else {
            return p1.id < p2.id;
        }
    });
}

int main() {
    int n;
    cin >> n;
    
    vector<pair<string, int>> posInfo{{"BangZhu", 1}, {"FuBangZhu", 2}, {"HuFa", 2},
        {"ZhangLao", 4}, {"TangZhu", 7}, {"JingYing", 25}, {"BangZhong", INT_MAX}};
    map<string, int> posMap{{"BangZhu", 0}, {"FuBangZhu", 1},
        {"HuFa", 2}, {"ZhangLao", 3}, {"TangZhu", 4}, {"JingYing", 5}, {"BangZhong", 6}};
    vector<People> info(n);
    for (int i = 0; i < n; ++i) {
        cin >> info[i].name >> info[i].position >> info[i].contribute >> info[i].grade;
        info[i].id = i;
        if (info[i].position == "BangZhu" || info[i].position == "FuBangZhu") {
            --posInfo[posMap[info[i].position]].second;
        } else {
            info[i].position.clear();
        }
    }
    
    adjust(posInfo, posMap, info);
    for (int i = 0; i < info.size(); ++i) {
        cout << info[i].name << " " << info[i].position << " " << info[i].grade << endl;
    }
    return 0;
}
上一篇:Java 基础(抽象类与抽象方法)


下一篇:抽象类和接口