code:
#include <set> #include <cstdio> #include <algorithm> #define N 200009 #define ll long long #define setIO(s) freopen(s".in","r",stdin) using namespace std; int n,K; int a[N],A[N],bu[N],arr[N],id[N],sum[N],pos[N]; int lowbit(int x) { return x&(-x); } void update(int x,int v) { for(;x<N;x+=lowbit(x)) sum[x]+=v; } int query(int x) { int re=0; for(;x;x-=lowbit(x)) re+=sum[x]; return re; } set<int>se; set<int>::iterator it; int main() { // setIO("input"); // freopen("input.out","w",stdout); scanf("%d%d",&n,&K); for(int i=1;i<=n;++i) { scanf("%d",&a[i]); A[i]=a[i]; } sort(A+1,A+1+n); for(int i=1;i<=n;++i) { a[i]=lower_bound(A+1,A+1+n,a[i])-A; } for(int i=1;i<=n;++i) { if(!bu[a[i]]) { bu[a[i]]=a[i]; } else ++bu[a[i]]; arr[i]=bu[a[i]]; id[arr[i]]=A[a[i]]; } for(int i=1;i<=n;++i) { int num=i-1-query(arr[i]); if(K<=num) { pos[i-K]=arr[i]; } else { se.insert(arr[i]); } update(arr[i],1); } for(int i=1;i<=n;++i) { if(pos[i]) printf("%d\n",id[pos[i]]); else { it=se.begin(); printf("%d\n",id[*it]); se.erase(it); } } return 0; }