1. 程式人生 > >PAT Advanced 1025 PAT Ranking

PAT Advanced 1025 PAT Ranking

color end begin rec fun records pub rds int

#include <cstdio>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <algorithm>

using namespace std;

class Record
{
public:
    string reg_num;
    int grade;
    int final_rank;
    int loc_num;
    int loc_rank;
};

vector<Record> records;

bool final_rank_func(const Record& a, const Record& b)
{
    if (a.grade == b.grade)
    {
        return a.reg_num < b.reg_num;
    }
    return a.grade > b.grade;
}

void prob1025()
{
    int group_num;
    scanf("%d", &group_num);
    for (int i = 0; i < group_num; ++i)
    {
        int group_size;
        scanf("%d", &group_size);
        for (int j = 0; j < group_size; ++j)
        {
            string reg_num;
            int grade;
            cin >> reg_num;
            scanf("%d", &grade);
            Record r;
            r.reg_num = reg_num;
            r.grade = grade;
            r.loc_num = i + 1;
            records.push_back(r);
        }
    }
    printf("%d\n", records.size());
    sort(records.begin(), records.end(), final_rank_func);

    map<int, int> group_last;
    map<int, int> group_size;
    for (size_t i = 0; i < records.size(); ++i)
    {
        int final_rank;
        if (i > 0 && records[i].grade == records[i - 1].grade)
        {
            final_rank = records[i - 1].final_rank;
        }
        else
        {
            final_rank = i + 1;
        }
        records[i].final_rank = final_rank;

        
        if (group_last.find(records[i].loc_num) != group_last.end())
        {
            if (records[i].grade == records[group_last[records[i].loc_num]].grade)
            {
                records[i].loc_rank = records[group_last[records[i].loc_num]].loc_rank;
            }
            else
            {
                records[i].loc_rank = group_size[records[i].loc_num] + 1;
            }
            ++group_size[records[i].loc_num];
        }
        else
        {
            group_size[records[i].loc_num] = 1;
            records[i].loc_rank = 1;
        }
        group_last[records[i].loc_num] = i;
        printf("%s %d %d %d\n", records[i].reg_num.c_str(), records[i].final_rank,
            records[i].loc_num,records[i].loc_rank);
    }
}

PAT Advanced 1025 PAT Ranking