Codeforces Round #590 (Div. 3)Social Network(双向容器)

原题:https://codeforces.com/contest/1234/problem/B2

B2. Social Network (hard version)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nnmessages.

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

Copy

7 2
1 2 3 2 1 3 2

output

Copy

2
2 1 

input

Copy

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

output

Copy

3
1 3 2 

Note

In the first example the list of conversations will change in the following way (in order from the first to last message):

  • [][];
  • [1][1];
  • [2,1][2,1];
  • [3,2][3,2];
  • [3,2][3,2];
  • [1,3][1,3];
  • [1,3][1,3];
  • [2,1][2,1].

In the second example the list of conversations will change in the following way:

  • [][];
  • [2][2];
  • [3,2][3,2];
  • [3,2][3,2];
  • [1,3,2][1,3,2];
  • and then the list will not change till the end.

题意:给出n,k,有n个数,只能显示k个数,如果此时已经显示了k个数,那么接下来的数如果和已经显示的数相同那么原来显示的不变,如果和显示的k个数中不一样,就删除最早的那一个,在把这一个储存在里面。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<iterator>
#include<vector>
#include<stdlib.h>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<sstream>
typedef long long ll;
using namespace std;

void Egriiiiiiiii()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

map<int,bool>mp ;
int main()
{
    Egriiiiiiiii() ;
    int n,k;
    cin>>n>>k;
    deque<int> dq;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        if(!mp[x]&&dq.size()==k){
            mp[x]=1;
            mp[dq.back()] = 0;
            dq.pop_back();
            dq.push_front(x);
        }
        if(!mp[x]&&dq.size()!=k){
            mp[x] = 1;
            dq.push_front(x);
        }
    }
    cout<<dq.size()<<endl;
    while(!dq.empty()){
        cout<<dq.front()<<" ";
        dq.pop_front();
    }
    return 0;
}
原创文章 96 获赞 28 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_43813140/article/details/102506940