双向链表()
题目:
补半年前的题=_=
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e5+10; 4 5 map<int,int> mp; 6 int L[maxn],R[maxn],val[maxn]; 7 int cnt=0; 8 char ans[maxn]; 9 10 void insert_(int id){ 11 R[id]=R[0]; 12 L[id]=0; 13 L[R[0]]=id; 14 R[0]=id; 15 } 16 17 void erase_(int id){ 18 R[L[id]]=R[id]; 19 L[R[id]]=L[id]; 20 } 21 22 int main(){ 23 int n,k; 24 while(scanf("%d%d",&n,&k)!=EOF){ 25 mp.clear(); 26 int x; 27 cnt=0; 28 R[0]=k+1; 29 L[k+1]=0; 30 int id; 31 for(int i=0;i<n;i++){ 32 scanf("%d",&x); 33 if(mp.count(x)){ 34 ans[i]='1'; 35 id=mp[x]; 36 erase_(id); 37 insert_(id); 38 }else{ 39 ans[i]='0'; 40 if(cnt<k){ 41 mp[x]=++cnt; 42 val[cnt]=x; 43 insert_(cnt); 44 }else{ 45 id=L[k+1]; 46 mp.erase(val[id]); 47 mp[x]=id; 48 erase_(id); 49 val[id]=x; 50 insert_(id); 51 } 52 } 53 } 54 ans[n]='\0'; 55 puts(ans); 56 } 57 return 0; 58 }