洛谷题单指南-模拟和高精度-P1786 帮贡排序

原题链接:https://www.luogu.com.cn/problem/P1786

题意解读:

此题比较简单,模拟+排序即可解决。需要注意的是,当帮贡或者等级相同时,都要保持原来的顺序,因此需要记录每个人的编号,便于排序。

话不多说,直接上代码。

100分代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 120;

struct Person
{
    int id;
    string name;
    string title;
    long long score;
    int level;
};

Person bangzhu;
vector<Person> fubangzhu;
vector<Person> hufa;
vector<Person> zhanglao;
vector<Person> tangzhu;
vector<Person> jingying;
vector<Person> bangzhong;

Person persons[N];
int cnt = 0;

//按帮贡排序
bool cmp1(Person p1, Person p2)
{
    if(p1.score == p2.score) return p1.id < p2.id;
    return p1.score > p2.score;
}

//按等级排序
bool cmp2(Person p1, Person p2)
{
    if(p1.level == p2.level) return p1.id < p2.id;
    return p1.level > p2.level;
}

void print(Person &p)
{
    cout << p.name << " " << p.title << " " << p.level << endl;
}

void printAll(vector<Person> &v)
{
    for(auto p : v) print(p);
}

int main()
{
    int n;
    cin >> n;

    for(int i = 0; i < n; i++)
    {
        Person p;
        p.id = i;
        cin >> p.name >> p.title >> p.score >> p.level;
        if(p.title == "BangZhu") bangzhu = p;
        else if(p.title == "FuBangZhu") fubangzhu.push_back(p);
        else persons[cnt++] = p;
    }

    //将除帮主、副帮主以外的人按帮贡排序,帮贡相同的保持原来的顺序
    sort(persons, persons + cnt, cmp1);

    //对每个人分配职位
    for(int i = 0; i < cnt; i++)
    {
        Person p = persons[i];
        if(i < 2) p.title = "HuFa", hufa.push_back(p);
        else if(i < 6) p.title = "ZhangLao", zhanglao.push_back(p);
        else if(i < 13) p.title = "TangZhu", tangzhu.push_back(p);
        else if(i < 38) p.title = "JingYing", jingying.push_back(p);
        else p.title = "BangZhong", bangzhong.push_back(p);
    }

    //输出信息
    print(bangzhu);
    sort(fubangzhu.begin(), fubangzhu.end(), cmp2);
    printAll(fubangzhu);
    if(hufa.size())
    {
        sort(hufa.begin(), hufa.end(), cmp2);
        printAll(hufa);
    }
    if(zhanglao.size())
    {
        sort(zhanglao.begin(), zhanglao.end(), cmp2);
        printAll(zhanglao);
    }
    if(tangzhu.size())
    {
        sort(tangzhu.begin(), tangzhu.end(), cmp2);
        printAll(tangzhu);
    }
    if(jingying.size())
    {
        sort(jingying.begin(), jingying.end(), cmp2);
        printAll(jingying);
    }
    if(bangzhong.size())
    {
        sort(bangzhong.begin(), bangzhong.end(), cmp2);
        printAll(bangzhong);
    }

    return 0;
}

 

posted @ 2024-01-24 14:54  五月江城  阅读(30)  评论(0编辑  收藏  举报