#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> PII; #define ls l,mid,rt<<1 #define rs mid+1,r,rt<<1|1 const int MAXN = 1e6+10; const double EPS = 1e-12; struct DLink{ DLink *Pre; DLink *Next; int key; DLink(int x) : key(x),Pre(NULL),Next(NULL){} }; int sz,cap; map<int,bool>mp; DLink *tail=new DLink(0),*head=new DLink(0); void add(int x){ DLink *now=new DLink(x); head->Next->Pre=now; now->Pre=head; now->Next=head->Next; head->Next=now; sz++; mp[x]=1; if(sz>cap){ mp[tail->Pre->key]=0; tail->Pre->Pre->Next=tail; tail->Pre=tail->Pre->Pre; sz--; } } void get(int x){ if(!mp[x]){ printf("Can't find this page. "); } DLink *now=head->Next; while(now->key!=x){ now=now->Next; } now->Pre->Next=now->Next; now->Next->Pre=now->Pre; head->Next->Pre=now; now->Pre=head; now->Next=head->Next; head->Next=now; } int main() { head->Next=tail; tail->Pre=head; scanf("%d",&cap); /*while(1){ 输入操作 增加页面:add(); 获取页面:get(); break; }*/ }
通过双端队列+map实现LRU