求调
查看原帖
求调
349824
WsW_花逝爆零人楼主2024/8/4 10:54
#include<bits/stdc++.h>
using namespace std;
struct node{
	int cnt,t,x;
	bool operator<(const node& a)const{
		if(a.cnt!=cnt)return cnt>a.cnt;
		return t>a.t;
	}
};
priority_queue<node>q;

bool f[1000003];
int add[1000003];

int n,m;
int ans;
int a[1000003];
map<int,int>mp;
int ml;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		cin>>a[i];
		int x;
		if(!mp[a[i]])x=mp[a[i]]=++ml;
		else x=mp[a[i]];
		if(q.size()<n){
			q.push({0,i,x});
			f[x]=1;
		}
		else{
			if(f[x]){
				add[x]++;
				ans++;
			}
			else{
				while(add[q.top().x]){
					node xx=q.top();
					q.pop();
					xx.cnt+=add[xx.x];
					add[xx.x]=0;
					q.push(xx);
				}
				f[q.top().x]=0;
				q.pop();
				q.push({0,i,x});
			}
		}
	}
	cout<<ans;
	return 0;
}
2024/8/4 10:54
加载中...