求明人指点
#include<bits/stdc++.h>
#define ll long long
#define pri priority_queue<ll>
#define prig priority_queue<ll, vector<ll>, greater<ll> >
using namespace std;
const int N = 5e5 + 1000;
struct node{
ll x, next;
bool operator < (const node &a) const{
return a.next > next;
}
};
ll n, m, k, ans;
ll a[N], vis[N], v[N];
priority_queue<node> q;
int main(){
cin >> n >> k >> m;
for (int i = 1; i <= m; i ++) cin >> a[i], v[i] = 1e8;
for (int i = m; i >= 1; i --){
q.push((node){a[i], v[a[i]]}), v[a[i]] = i;
}
for (int i = 1, cnt = 0; i <= m; i ++){
if (!vis[a[i]] && cnt < k){
vis[a[i]] = 1;
cnt ++;
ans ++;
}else if (!vis[a[i]] && cnt == k){
vis[q.top().x] = 0, q.pop();
vis[a[i]] = 1;
ans ++;
}
}
cout << ans;
return 0;
}
采用 堆优化 + 贪心
后面全wa了