We need to simulate the behavior of a smartphone's recent conversations list, which is a classic "LRU" cache (Least Recently Used) with capacity k. When a message from a friend is received, if that friend's conversation is already in the list, we bring it to the front? Wait: According to the description:
- If the conversation is currently displayed on the smartphone, then nothing happens: the conversations do not change and do not change their order. So if it's already on the screen, we do NOT move it to the top; it stays in place.
But typical problem "Messaging" (like from Codeforces problem 115A? Actually it's Codeforces problem 1288A? No. I recall a problem: "Social Network (easy version/hard version)" that involves a smartphone showing recent conversations. Actually, I think it's Codeforces Round #1199 problem? Let's think. The problem described is "Social Network (easy/hard)" from Codeforces Round #... There is an easy version with constraints n,k up to 200, and hard version up to 2e5. So here it's the easy version, but we can simulate with O(n*k) or O(n^2) because n,k <=200.
But note the behavior: The only time we change the list is when we receive a message from a friend whose conversation is not currently displayed. Then we remove the last (kth) conversation if the list size is k, then we add the new conversation at the top (position 1), shifting others down.
Important: When we receive a message from a friend already displayed, we do nothing. So no reordering occurs. So this is not a typical LRU that moves accessed items to the front. It's like "if new message from a friend not on screen, bring it to the front (evict if needed)". Accesses to items on screen do not affect order. So it's like a cache with "new items are inserted at the front, but existing items stay put."
That is exactly the described process.
Thus we need to simulate for n up to 200. That's trivial: maintain a list (like a Python list) of displayed conversations in order (from topmost to bottommost). For each id in the sequence:
- If id is already in the list: do nothing.
- Else:
- If the length of list == k: pop the last element (if k>0; but k >=1? constraints: n,k >=1. But k could be less than n). But careful: if list length is k, we remove the last. Then we insert id at position 0 (top). Alternatively, we could append to front then truncate but we need to ensure we don't exceed k.
At the end, we output the list length (which will be at most min(n,k)) and the list itself.
Edge cases: If k = 0? But constraints say k>=1, so fine.
But the sample output shows for first example with n=7, k=2: final list [2,1]. Our simulation: start empty.
- 1: not in list, list empty -> insert at top -> [1]
- 2: not in list, size 1<k=2 -> insert at top -> [2,1]
- 3: not in list, size=2 == k -> remove last (1) -> list becomes [2]; then insert 3 at top -> [3,2]
- 2: is in list -> do nothing (list stays [3,2])
- 1: not in list, size=2 -> remove last (2) -> list becomes [3]; insert 1 -> [1,3]
- 3: is in list -> nothing (list [1,3])
- 2: not in list, size=2 -> remove last (3) -> list becomes [1]; insert 2 -> [2,1]
So final [2,1], matches.
Second example: n=10, k=4, ids: 2 3 3 1 1 2 1 2 3 3. Simulate:
- 2: not, list [] -> [2]
- 3: not -> [3,2]
- 3: already -> [3,2]
- 1: not -> [1,3,2]
- 1: already -> [1,3,2]
- 2: already -> [1,3,2] (since 2 is present)
- 1: already -> [1,3,2]
- 2: already -> [1,3,2]
- 3: already -> [1,3,2]
- 3: already -> [1,3,2]
Wait the output says final list is [1,3,2] length 3. That matches.
But note: In the problem statement output of second example is:
3
1 3 2
Yes.
So simple.
But the constraints are up to 200, so brute force works.
Now we need to write solution in a generic way, but we should note that there is also a hard version (with n,k up to 2e5) which requires O(n) solution using a double-ended queue and hash set for quick lookup, but we don't need to implement that for easy version. However, we can still implement an O(n*k) simulation using list and index (or using Python list with remove? That would be O(k) for checking membership and removing if needed. Since k<=200, that's fine.
Better to use list for the order, and a set for quick membership? But membership test in Python list is O(k) which is fine for 200. But for consistency, we can use set for O(1) check. But we need to maintain order; we can store list of ids. When we need to remove the last, we pop and also remove from set. And when we insert at front, we insert into set and list. When id is already present, we do nothing. However, careful: Because if we only maintain set, it's fine but we also need to ensure we don't accidentally remove the id from set when it's still in list. The simulation is straightforward.
We can just use a deque from collections? But we need to check if id is present. With deque we could check using membership which is O(k). That's fine. But maybe we can use a list.
Simplify: Let 'display' be a list representing the screen from top to bottom.
For each id:
if id in display:
continue
else:
if len(display) == k:
display.pop() # remove last
display.insert(0, id)
At the end, print len(display) and ' '.join(map(str, display))
But note: Insert at index 0 is O(k) because shifting. But k <= 200, fine.
Alternatively, we could reverse the order (store from bottom to top) to make appending easier? But no, it's fine.
Check edge: if k=0? But constraints n,k>=1 so ignore.
If n=0? n>=1.
Thus solution done.
But the user asked "The only difference between easy and hard versions are constraints on n and k." They just gave the problem statement. So answer with the solution for easy version.
We need to produce output: In the first line integer m, and in second line the IDs.
Make sure to not print trailing spaces.
Now I'll write final answer: Python code.
Implementation:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
k = int(next(it))
ids = [int(next(it)) for _ in range(n)]
display = [] # topmost first
for id in ids:
if id in display:
continue
if len(display) == k:
display.pop() # remove last
display.insert(0, id)
print(len(display))
print(' '.join(map(str, display)))
But careful: In Python, 'in' on list is O(m) but m <= min(k, n) <= 200, fine.
Potential issue: id values up to 1e9, but that's fine.
Test with sample.
Sample 1: n=7,k=2, ids 1 2 3 2 1 3 2 => display after simulation [2,1], length 2. Prints "2\n2 1". The sample output shows "2\n2 1 ". Trailing space not required. So fine.
Sample 2: n=10,k=4 => prints "3\n1 3 2". Good.
But the problem states output format: "In the second line print m integers ids_1, ids_2, ..., ids_m". Usually we don't have trailing spaces.
Thus final answer.
But the user may be expecting a solution in C++? They didn't specify language. However, the context of Codeforces problems often accept any language. But as ChatGPT, we can give a solution in Python.
We can also describe the algorithm.
But the user might be wanting the solution from the problem "Social Network (easy version)"? They posted the problem statement. They'd like to see the code.
I'll output a Python code.
But also note that we need to handle input reading properly. Provide code with function solve() and main guard.
Thus answer.
We could also do with deque and set for better performance but not necessary.
I'll include comments.
Now output final. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
k = int(next(it))
ids = [int(next(it)) for _ in range(n)]
screen = [] # topmost conversation first
for friend in ids:
if friend in screen:
continue
if len(screen) == k:
screen.pop() # remove the last (position k)
# insert new conversation at the top
screen.insert(0, friend)
# output
print(len(screen))
print(' '.join(map(str, screen)))
if name == "main":
solve()