思路: 利用map+list暴力模拟就彳于了
#pragma GCC optimize(2) #include <bits/stdc++.h> using namespace std; #define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define MAXN 100001 #define INF (0x3f3f3f3f) #define uint unsigned int #define ll long long #define ull unsigned long long #define lowbit(x) ((x) & -(x)) #define DEBUG(a) cout<<#a<<" = "<<(a)<<endl; //head typedef struct Node{ string id; int var; Node() {} Node(const string &_id, const int &_var): id(_id), var(_var) {} }Node; list<Node> link; unordered_map<string,list<Node>::iterator> mmp; uint m, M; //m 当前 cache 大小; M 块大小 void update(string name, int var){ if(mmp.find(name) == mmp.end()){ link.push_back(Node(name, var)); mmp.insert({name, --link.end()}); if(m == M){ mmp.erase(link.front().id); link.pop_front(); }else{ ++m; } cout << var <<'\n'; }else{ auto it = mmp[name]; cout << it->var <<'\n'; link.push_back(Node(name, it->var)); link.erase(it); mmp[name] = --link.end(); } } void query(string name, int var){ if(mmp.find(name) == mmp.end()){ cout << "Invalid\n"; }else{ auto it = mmp[name]; if(var == 1){ it = next(it); if(it == link.end()){ cout << "Invalid\n"; }else{ cout << it->var << '\n'; } }else if(var == -1){ if(it == link.begin()){ cout << "Invalid\n"; }else{ it = prev(it); cout << it->var << '\n'; } }else{ cout << it->var << '\n'; } } } void solve(){ link.clear(); mmp.clear(); m = 0; int Q; cin >> Q >> M; for(int i = 0;i < Q;++i){ int op, var; string name; cin >> op >> name >> var; if(op){ query(name, var); }else{ update(name, var); } } } int main(){ IOS int T; cin >> T; while(T--){ solve(); } return 0; }View Code
标签:map,management,name,int,list,mmp,link,var,define From: https://www.cnblogs.com/Lamboofhome/p/17307305.html