【Daily Code AcWing】 PAT 最佳排名

文章讲述了作者在使用C++STL库中的map数据结构时遇到的问题,主要涉及到自动排序的key而非value以及map查询key对应下标的困难。通过实例代码展示了如何查找和处理map中的数据,并对比了与unordered_map的使用差异。
摘要由CSDN通过智能技术生成

这回自己的代码是彻彻底底用不了……原因为以下:

  1. map自动排序……但是我没有搞明白是通过key排序!而不是我想的value!!
  2. map查询的时候不能找到key对应的下标!只能通过遍历查到!!!
    对于STL库的数据结构不是很了解哈……等下抄完别人的代码总结一下map的用法好了QAQ
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;
const int N = 2e3 + 10;
int n, m;
struct student {
    string name;
    int rank;
    string w;
    int C, M, E, A;
}s[N];

map<string, int> av;
map<string, int> cv;
map<string, int> mv;
map<string, int> ev;


int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i ++ ) {
        cin >> s[i].name >> s[i].C >> s[i].M >> s[i].E >> s[i].A;
        cv[s[i].name] = s[i].C;
        mv[s[i].name] = s[i].M;
        ev[s[i].name] = s[i].E;
        av[s[i].name] = s[i].A;
    }
    
    for (int i = 0; i < m; i ++ ) {
        string ask;
        cin >> ask;
        auto ita = av.find(ask);
        if(ita == av.end()) {
            printf("N/A\n");
            continue;
        }
        
        auto itc = cv.find(ask);
        auto itm = mv.find(ask);
        auto ite = ev.find(ask);
        
        auto max1 = ita - av.begin();
        int select1;
        if(ita - av.begin() >= itc - cv.begin()) {
            select1 = 1;
        }
        else select1 = 2;
        max1 = max(ita - av.begin(), itc - cv.begin());
        
        auto max2 = itm - mv.begin();
        int select2;
        if(itm - mv.begin() >= ite - ev.begin()) {
            select2 = 3;
        }
        else select2 = 4;
        max2 = max(itm - mv.begin(), ite - ev.begin());
        
        if(max2 > max1) {
            select1 = select2;
        }
        
        cout << max1 << " ";
        if(select1 == 1) cout << "A"  << endl;
        else if(select1 == 2) cout << "C" << endl;
        else if(select1 == 3) cout << "M" << endl;
        else if(select1 == 4) cout << "E" << endl;

    }
    
        
    return 0;
}

以下是抄的y总的代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <unordered_map>
#include <cmath>

using namespace std;

unordered_map<string, vector<int>> grades;
vector<int> q[4];

int get_rank(vector<int> &a, int x) {
    int l  = 0, r = a.size() - 1;
    while(l < r) {
        int mid = (l + r + 1) >> 1;
        if(a[mid] <= x) l = mid;
        else r = mid - 1;
    }
    return a.size() - r;
}

int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i ++ ) {
        string id;
        int t[4] = {0};
        cin >> id;
        for (int j = 1; j < 4; j ++ ) {
            cin >> t[j];
            t[0] += t[j];
        }
        t[0] = round(t[0] / 3.0);
        for (int j = 0; j < 4; j ++ ) {
            q[j].push_back(t[j]);
            grades[id].push_back(t[j]);
        }
        
    }
    
    
    for (int i = 0; i < 4; i ++ ) sort(q[i].begin(), q[i].end());
    
    char names[] = "ACME";
    
    while(m -- ) {
        string id;
        cin >> id;
        if(grades.count(id) == 0) puts("N/A");
        else {
            int res = n + 1;
            char c;
            for (int i = 0; i < 4; i ++ ) {
                int rank = get_rank(q[i], grades[id][i]);
                if(rank < res) {
                    res = rank;
                    c = names[i];
                }
            }
            cout << res << " " << c << endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值