(离散化)Codeforces Round #590 (Div. 3) B2 Social Network

题目

The only difference between easy and hard versions are constraints on nn and kk.

You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most kk most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 00).

Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.

You (suddenly!) have the ability to see the future. You know that during the day you will receive nn messages, the ii-th message will be received from the friend with ID idiidi (1≤idi≤1091≤idi≤109).

If you receive a message from idiidi in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.

Otherwise (i.e. if there is no conversation with idiidi on the screen):

  • Firstly, if the number of conversations displayed on the screen is kk, the last conversation (which has the position kk) is removed from the screen.
  • Now the number of conversations on the screen is guaranteed to be less than kk and the conversation with the friend idiidi is not displayed on the screen.
  • The conversation with the friend idiidi appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.

Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all nn messages.

Input

The first line of the input contains two integers nn and kk (1≤n,k≤2⋅105)1≤n,k≤2⋅105) — the number of messages and the number of conversations your smartphone can show.

The second line of the input contains nn integers id1,id2,…,idnid1,id2,…,idn (1≤idi≤1091≤idi≤109), where idiidi is the ID of the friend which sends you the ii-th message.

Output

In the first line of the output print one integer mm (1≤m≤min(n,k)1≤m≤min(n,k)) — the number of conversations shown after receiving all nn messages.

In the second line print mm integers ids1,ids2,…,idsmids1,ids2,…,idsm, where idsiidsi should be equal to the ID of the friend corresponding to the conversation displayed on the position ii after receiving all nn messages.

Examples

input

7 2
1 2 3 2 1 3 2

output

2
2 1 

input

10 4
2 3 3 1 1 2 1 2 3 3

output

3
1 3 2 

一开始开了bool 型1e9的数组,爆内存了(果然还是太年轻了),大佬们用的都是离散化和map,我百度了下离散化发现不太懂,但从离散化想到了这个方法,这样最多就只需要2e5的数组来标记

#include <bits/stdc++.h>  
using namespace std;  
#define ll long long
const int maxn=1e9+5;
int a[200005],c[200005],f[200005];//a记录值,c记录排名,f记录是否存在
struct cc{
	int v,id,x;//v记录值,id记录输入顺序,x记录值的大小排名
}b[200005];
bool cmp1(cc x,cc y)
{
	return x.v<y.v;
}
bool cmp2(cc x,cc y)
{
	return x.id<y.id;
}
int main ()
{
	int n,k,x=0,m,i,s=0;
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&b[i].v);
		b[i].id=i;
	}
	sort(b+1,b+1+n,cmp1);
	for(int i=1;i<=n;i++)
	{
		if(b[i].v!=b[i-1].v)//相同元素,排名相同
			x++;
		b[i].x=x;
	}
	sort(b+1,b+1+n,cmp2);
	x=0;                    //x标记队首
	for(int xx=1;xx<=n;xx++)
	{
		if(f[b[xx].x]==0)
		{
			f[c[x]]=0;
			a[x]=b[xx].v;
			c[x]=b[xx].x;
			f[b[xx].x]=1;
			x=(x+1)%k;        //更新x
		}
	}
	if(x==0)
		x=k-1;
	else
		x--;
	for(int i=0;i<k;i++)
		if(a[i])
			s++;
	cout<<s<<endl;
	printf("%d",a[x]);
	for(int i=1;i<k;i++)
	{
		if(a[(x-i+k)%k]==0)
			break;
		printf(" %d",a[(x-i+k)%k]);
	}
    return 0;  
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值