标签:des blog io ar os sp for on div
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<ctime>
#include<ext/pb_ds/priority_queue.hpp>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
using namespace __gnu_pbds;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
return x*f;
}
int n,k,p,now=1,ans;
int a[500010];
int lst[100010];
int nex[500010];
bool mrk[100010];
__gnu_pbds::priority_queue <pa >q;
int main()
{
n=read();k=read();p=read();
for (int i=1;i<=p;i++)a[i]=read();
for (int i=1;i<=n;i++)lst[i]=p+1;
for (int i=p;i>=1;i--)
{
nex[i]=lst[a[i]];
lst[a[i]]=i;
}
ans=k;
while (k&&now<=p)
{
q.push(make_pair(nex[now],a[now]));
if (!mrk[a[now]])k--;
mrk[a[now]]=1;
now++;
}
for (int i=now;i<=p;i++)
{
if (mrk[a[i]])
{
q.push(make_pair(nex[i],a[i]));
continue;
}
while (!mrk[q.top().second])q.pop();
int fst=q.top().first,cnd=q.top().second;
q.pop();
mrk[cnd]=0;
q.push(make_pair(nex[i],a[i]));
mrk[a[i]]=1;
ans++;
}
printf("%d\n",ans);
return 0;
}
bzoj1528 [POI2005]sam-Toy Cars
标签:des blog io ar os sp for on div
原文地址:http://www.cnblogs.com/zhber/p/4150496.html