比赛的时候题意看不懂。。
然后就卡了。。
L题
就是模拟操作系统的LRU算法。
就是维护一个链表,如果要替换的话就找到最后一个替换,如果要插入的话就插入到开头,如果在链表中已经存在的话,就把这个提到链表的开头,然后搞一下就行了。
看了别人的做法, 用的是queue+map
cnt与queue中的元素个数不是同步的 。’不过我觉得这样不是那么容易想到。。
标程, 应该是双向链表吧。。
//自己应该要会用map标记某个数出现的位置。。我居然想的时候用for查一遍
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MaxN=100005;
int N,K;
map <int,int> rem;
int LL[MaxN],RR[MaxN],val[MaxN];
int cou;
char ans[MaxN];
inline void erase(int p) {
RR[LL[p]]=RR[p];
LL[RR[p]]=LL[p];
}
inline void insert(int p) {
RR[p]=RR[0];
LL[p]=0;
LL[RR[0]]=p;
RR[0]=p;
}
int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int a,b;
while(~scanf("%d %d",&N,&K)) {
cou=0;
RR[0]=K+1;
LL[K+1]=0;
rem.clear();
ans[N]=0;
for(int i=0;i<N;++i) {
scanf("%d",&a);
if(rem.count(a)) {
ans[i]='1';
b=rem[a];
erase(b);
insert(b);
}
else {
ans[i]='0';
if(cou<K) {
++cou;
rem[a]=cou;
val[cou]=a;
insert(cou);
}
else {
b=LL[K+1];
rem.erase(val[b]);
rem[a]=b;
erase(b);
val[b]=a;
insert(b);
}
}
}
puts(ans);
}
return 0;
}