72分求调qwq
查看原帖
72分求调qwq
1389490
ly_ld楼主2025/7/25 20:30
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;

ll n,k,p;
ll a[N];
ll last[N];
ll next[N];
ll ans;
bool vis[N];
struct node{
	ll nxt,id;
	
	bool operator < (const node&x) const{
		return nxt < x.nxt;
	}
};

priority_queue <node> q;


int main(){
	cin >> n >> k >> p;
	for(int i=1;i<=p;i++) cin>>a[i];
	
	for(int i=p;i>=1;i--){
		if(!last[a[i]]) next[i] = 1e18+5;
		else next[i] = last[a[i]]; 
		last[a[i]]=i;
	}
	
	for(int i=1;i<=p;i++){
		
		if(vis[a[i]]){
			k++;
			node nw;
			nw.nxt = next[i];
			nw.id = a[i];
			q.push(nw);
		}
		else{
			if(q.size()>=k) vis[q.top().id]=0,q.pop();
			node nw;
			nw.nxt = next[i];
			nw.id = a[i];
			q.push(nw);
			vis[a[i]]=1;
			ans++;
		}	
	}
	cout << ans;
}
2025/7/25 20:30
加载中...