CodeForces - 1234B2 Social Network (hard version)

CodeForces - 1234B2 Social Network (hard version)

题目

The only difference between easy and hard versions are constraints on n and k .
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).

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 i-th message will be received from the friend with ID idi (1≤idi≤109).

If you receive a message from idi 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 idi on the screen):

Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
Now the number of conversations on the screen is guaranteed to be less than k 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 n messages.


Input

The first line of the input contains two integers n and k (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 ,…,idn (1≤idi ≤109), where idi is the ID of the friend which sends you the i-th message.


Output

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

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


Sample Input

7 2
1 2 3 2 1 3 2

Sample Output

2
2 1 

Sample Input

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

Sample Output

3
1 3 2 

题目大意:
一共有n条信息,屏幕上只能显示k个好友的信息,如果该好友的信息已经在屏幕上则不发生任何变化,否则将最早的一条好友信息顶掉,新的好友信息进入屏幕。

题目分析:

与简单版相比,复杂版 就是 n,k的范围比较大, 我们 在 main 函数外开数组,并且用map来 解决,用朋友的ID做map的下标,当屏幕上有时,不执行操作,当没有时 map里会直接创建这个为下标的“朋友”,剩下就是操作 屏幕,具体看代码,有解释,
(写完发现,好像其实不用 map也可以 )
简单版请移步 : CodeForces - 1234B1 Social Network (easy version)

AC代码

#include<bits/stdc++.h>
# include <stdio.h>
using namespace std;
map<int,int>mp;
int a[20000000];//这个数组表示屏幕,

int main()
{
	int n,k,b,j=0,i;
    scanf("%d%d",&n,&k);
    for( i=1;i<=n;++i)
    {
        
        scanf("%d",&b);
        if(mp[b])//说明 mp[b]存在,
		continue;
        
		mp[b]=1;//map中元素的查找和读取,用下标的方法读取map中元素时,若map中不存在该元素,则会在map中插入。
        a[++j]=b;//在屏幕里 加入这个 ID 
        if(j>k)
        {
            mp[a[j-k]]=0; //map里这个 ID 清 0, 即不在屏幕上了
        }
    }
    printf("%d\n",min(j,k));
    for( i=j;i>=max(j-k+1,1);--i)//max是保证了 j<k的情况
    {
        printf("%d ",a[i]);//因为屏幕数组我们是顺着存放的,所以我们需要倒着输出,
    }
    return 0;
}

我来要赞了,如果觉得解释还算详细,可以学到点什么的话,点个赞再走吧
欢迎各位路过的dalao 指点,提出问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值