bzoj1206-[HNOI2005]虚拟内存

卡读的毒瘤题==

看懂之后用map模拟.或者线段树

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
#define rep(i,l,r) for(register int i=(l);i<=(r);++i)
#define repdo(i,l,r) for(register int i=(l);i>=(r);--i)
#define il inline
typedef double db;
typedef long long ll;

//---------------------------------------
int n,m,ans;
struct tnd{int cnt,t;};
bool operator<(tnd l,tnd r){return l.cnt!=r.cnt?l.cnt<r.cnt:l.t<r.t;}
map<int,tnd> name;
map<tnd,int> cs;
int sz;
bool sol(int v,int t){
	if(name.find(v)!=name.end()){
		tnd tmp=name[v];
		cs.erase(tmp);
		tmp.cnt++;
		name[v]=tmp,cs[tmp]=v;
		return 1;
	}
	else if(sz<n){
		tnd tmp=(tnd){1,t};
		name[v]=tmp,cs[tmp]=v;
		++sz;
		return 0;
	}
	else{
		int v0=cs.begin()->second;
		name.erase(v0),cs.erase(cs.begin());
		tnd tmp=(tnd){1,t};
		name[v]=tmp,cs[tmp]=v;
		return 0;
	}
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0);
	cin>>n>>m;
	int a;
	rep(i,1,m){
		cin>>a;
		if(sol(a,i))++ans;
	}
	cout<<ans<<'
';
	return 0;
}
原文地址:https://www.cnblogs.com/ubospica/p/9871223.html