记录一个菜逼的成长。。
要先预处理一下每一个值的下一个相同值的位置。
注意要把各元素放在一个结构里,在用优先队列进行排序。
之前分开放一直过不了。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define cl(a,b) memset(a,b,sizeof(a))
#define clr clear()
#define pb push_back
#define fin freopen("D://in.txt","r",stdin)
#define fout freopen("D://out.txt","w",stdout)
const int INF = 0x3f3f3f3f;
const int maxn = 100000 + 10;
int a[maxn*5],last[maxn*5],Next[maxn*5],vis[maxn];
vector<int>pos[maxn];
void init()
{
for( int i = 0; i < maxn; i++ )
pos[i].clr;
cl(last,-1);cl(vis,-1);
cl(Next,INF);
}
struct Node{
int nx,v,pos;
Node(){}
Node(int aa,int bb,int cc){nx = aa,v = bb;pos = cc;}
friend bool operator < (const Node& a,const Node& b){
if(a.nx != b.nx)return a.nx < b.nx;
return a.v < b.v;
}
};
int main()
{
//fin,fout;
int n,m;
while(~scanf("%d%d",&n,&m)){
init();
vector<int>b(5*maxn,-1);
for( int i = 1; i <= n; i++ ){
scanf("%d",a+i);
if(pos[a[i]].size()){
last[i] = pos[a[i]][pos[a[i]].size()-1];
Next[last[i]] = i;
}
pos[a[i]].pb(i);
}
// for( int i = 1; i <= n; i++ ){
// printf("%d %d %d %d\n",i,a[i],last[i],Next[i]);
// }
int ind = 0;
priority_queue<Node>q;
for( int i = 1; i <= n; i++ ){
if(ind < m){
if(vis[a[i]] == -1)b[ind] = a[i],vis[a[i]] = ind++;
}
else {
if(vis[a[i]] == -1){
Node t = q.top();q.pop();
vis[t.v] = -1;
b[t.pos] = a[i];
vis[a[i]] = t.pos;
}
}
q.push(Node(Next[i],a[i],vis[a[i]]));
}
for( int i = 0; i < ind; i++ ){
if(!i)printf("%d",b[i]);
else printf(" %d",b[i]);
}
puts("");
}
return 0;
}