暑假前专题题解---数据结构---E


 

简单的模拟,用stl即可,pair在第一个值相等情况下会对第二个排序,自己写struct ,重载运算符也可;

#include <bits/stdc++.h>
using namespace std;
map<string, int> mp;
set<pair<int, string> > s;
int main(){
    int n,opt,pric;
    string name;
    scanf("%d",&n);
    for (int i=0; i<n; i++) {
        scanf("%d",&opt);
        if (opt == 1){
            cin >> name >> pric;
            if (mp.count(name)) continue;
            mp[name] = pric; s.insert(make_pair(pric, name));
        }else if (opt == 2){
            cin >> name;
            if (mp.count(name)){
                s.erase(make_pair(mp[name], name));
                mp.erase(name);
            }
        }else if (opt == 3){
            cin >> name >> pric;
            if (mp.count(name)){
                s.erase(make_pair(mp[name], name));
                mp[name] = pric;
                s.insert(make_pair(pric, name));
            }
        }else if (opt == 4){
            cin >> pric;
            if (s.empty()) continue;
            if (pric == 1){
                set<pair<int, string> >::iterator it = s.begin();
                cout << it->second << endl;
            }else if (pric == 2){
                set<pair<int, string> >::iterator it = s.end(); it--;
                cout << it->second << endl;
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/CCCCTong/article/details/81382783