codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1234B2.md (3166B)


      1 Source: https://codeforces.com/problemset/problem/1234/B2
      2 
      3 B2. Social Network (hard version)
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 The only difference between easy and hard versions are constraints on $n$ and $k$.
     22 
     23 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$).
     24 
     25 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.
     26 
     27 You (suddenly!) have the ability to see the future. You know that during the day you will receive $n$ messages, the $i$-th message will be received from the friend with ID $id_i$ ($1 \le id_i \le 10^9$).
     28 
     29 If you receive a message from $id_i$ 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.
     30 
     31 Otherwise (i.e. if there is no conversation with $id_i$ on the screen):
     32 
     33 * 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.
     34 * Now the number of conversations on the screen is guaranteed to be less than $k$ and the conversation with the friend $id_i$ is not displayed on the screen.
     35 * The conversation with the friend $id_i$ appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
     36 
     37 Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all $n$ messages.
     38 
     39 Input
     40 
     41 The first line of the input contains two integers $n$ and $k$ ($1 \le n, k \le 2 \cdot 10^5)$ — the number of messages and the number of conversations your smartphone can show.
     42 
     43 The second line of the input contains $n$ integers $id_1, id_2, \dots, id_n$ ($1 \le id_i \le 10^9$), where $id_i$ is the ID of the friend which sends you the $i$-th message.
     44 
     45 Output
     46 
     47 In the first line of the output print one integer $m$ ($1 \le m \le min(n, k)$) — the number of conversations shown after receiving all $n$ messages.
     48 
     49 In the second line print $m$ integers $ids_1, ids_2, \dots, ids_m$, where $ids_i$ should be equal to the ID of the friend corresponding to the conversation displayed on the position $i$ after receiving all $n$ messages.
     50 
     51 Examples
     52 
     53 Input
     54 
     55 ```
     56 7 2
     57 1 2 3 2 1 3 2
     58 ```
     59 
     60 Output
     61 
     62 ```
     63 2
     64 2 1
     65 ```
     66 
     67 Input
     68 
     69 ```
     70 10 4
     71 2 3 3 1 1 2 1 2 3 3
     72 ```
     73 
     74 Output
     75 
     76 ```
     77 3
     78 1 3 2
     79 ```
     80 
     81 Note
     82 
     83 In the first example the list of conversations will change in the following way (in order from the first to last message):
     84 
     85 * $[]$;
     86 * $[1]$;
     87 * $[2, 1]$;
     88 * $[3, 2]$;
     89 * $[3, 2]$;
     90 * $[1, 3]$;
     91 * $[1, 3]$;
     92 * $[2, 1]$.
     93 
     94 In the second example the list of conversations will change in the following way:
     95 
     96 * $[]$;
     97 * $[2]$;
     98 * $[3, 2]$;
     99 * $[3, 2]$;
    100 * $[1, 3, 2]$;
    101 * and then the list will not change till the end.