题意:
有m种任务,id 1~m,每种任务需要相同id的人来完成,但是同一时间只能拥有n个人,开始时无人,当遇到没有当前任务对应的人时,需要换人或者加人,问最少需要加或换多少人
解析:
对于每个任务,处理出下一次出现这个任务的时间,在换人的时候优先换最后面才需要的人
代码:
#define N 50009
D read(){ D ans=0; char last=' ',ch=getchar();
while(ch<'0' || ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans; return ans;
}
int n,m,q;
int nex[N];//nex[3]=5 和第3个数相同的是第5个数
int a[N];
struct node{
int pos,val;
node(int pos,int val):pos(pos),val(val){}
bool operator <(const node &k)const{
return nex[pos]<nex[k.pos];
}
};
int have[N];
int main(){
while(cin>>n>>m>>q){
priority_queue<node>Q;
map<int,int>tmp;
for(int i=1;i<=q;i++){
a[i]=read();
}
for(int i=q;i>=1;i--){
if(tmp[a[i]]==0)tmp[a[i]]=i,nex[i]=q+1;
else nex[i]=tmp[a[i]],tmp[a[i]]=i;
}
mmm(have,0);
int ans=0;
for(int i=1;i<=q;i++){
if(have[a[i]])continue;
ans++;
if(Q.size()<n){
Q.push(node(i,a[i]));
have[a[i]]=1;continue;
}
node tt=Q.top();Q.pop();
have[tt.val]=0;
Q.push(node(i,a[i]));
have[a[i]]=1;
}
printf("%d\n",ans);
}
}