liuchuo / PAT

🍭 浙江大学PAT题解(C/C++/Java/Python) - 努力成为萌萌的程序媛~
3.37k stars 824 forks source link

[Advanced/C++/1012] Suggestion #133

Open xwcai98 opened 4 years ago

xwcai98 commented 4 years ago

问题: 平均数只供排序,实际上与总和等价,所以用总和就可以了。作者采用的四舍五入取整的方法计算平均数,其实并不正确,因为题目没有明确提及,能被卡掉的。

另外,作者的代码属于预处理所有排名,代码很简洁,方法也很不错。这里提供一种不用预处理,在线处理的方法,类似离散化的思想(个人认为代码更加优秀)。

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    vector<char> type = {'A', 'C', 'M', 'E'};
    int n, m;
    cin >> n >> m;
    map<string, vector<int>> mp;
    vector<vector<int>> rank(4);
    for (int i = 0; i < n; ++i) {
        string id;
        cin >> id;
        vector<int> tmp(4);
        for (int j = 1; j < 4; ++j) {
            cin >> tmp[j];
            tmp[0] += tmp[j];
        }
        mp[id] = tmp;
        for (int j = 0; j < 4; ++j) 
            rank[j].push_back(tmp[j]);
    }
    for (int i = 0; i < 4; ++i)
        sort(rank[i].begin(), rank[i].end());
    while (m--) {
        string id;
        cin >> id;
        if (mp.find(id) == mp.end()) {
            cout << "N/A\n";
            continue;
        }
        vector<int> my_rank(4);
        for (int i = 0; i < 4; ++i) {
            auto ptr = upper_bound(rank[i].begin(), rank[i].end(), mp[id][i]);
            my_rank[i] = rank[i].end() - ptr + 1;
        }
        int pos = min_element(my_rank.begin(), my_rank.end()) - my_rank.begin();
        cout << my_rank[pos] << ' ' << type[pos] << '\n';
    }
    return 0;
} 
warmingkkk commented 3 years ago

有一说一确实