A - 重複除去ログ / Deduplicated Log 解説 by admin
Claude 4.6 Opus (Thinking)Overview
This problem involves looking through event logs arranged in chronological order from the beginning, and skipping any event code that is the same as the most recently recorded one (removing consecutive duplicates).
Analysis
Key Insight
What this problem asks for is “removing consecutive identical elements (run-length encoding style compression).” Note that this is not about removing all duplicates globally, but rather only checking whether the current element matches the tail of the recorded list.
For example, if the input is 1 1 2 2 1 1 3:
| Log being checked | Tail of recorded list | Action | Recorded list |
|---|---|---|---|
| 1 | (empty) | Add | [1] |
| 1 | 1 | Skip | [1] |
| 2 | 1 | Add | [1, 2] |
| 2 | 2 | Skip | [1, 2] |
| 1 | 2 | Add | [1, 2, 1] |
| 1 | 1 | Skip | [1, 2, 1] |
| 3 | 1 | Add | [1, 2, 1, 3] |
The final output is 1 2 1 3. The key point is that 1 appears again at a distant position, so it is recorded twice.
Is a Naive Approach Sufficient?
Since we only need to “compare each log with the previous element,” this can be solved in a single loop. Although \(N\) can be up to \(5 \times 10^5\), \(O(N)\) is more than fast enough. No special data structures or algorithms are needed.
Algorithm
- Prepare a list
resultto store the results, and a variableprevto hold the most recently added element (initializeprevtoNoneto represent “nothing has been added yet”). - Examine \(A_1, A_2, \ldots, A_N\) in order.
- If the current event code \(A_i\) is different from
prev, add it toresultand updateprevto \(A_i\). - If they are the same, do nothing (skip it).
- If the current event code \(A_i\) is different from
- Output the contents of
resultseparated by spaces.
Complexity
- Time complexity: \(O(N)\) — Each log is examined exactly once.
- Space complexity: \(O(N)\) — In the worst case (no duplicates at all), the recorded list has size \(N\).
Implementation Notes
Fast I/O: In Python, reading all input at once as bytes using
sys.stdin.buffer.read()and splitting withsplit()is fast. In the solution code, event codes are compared and concatenated as byte strings (without converting toint). Since no arithmetic operations on integer values are needed, handling them as strings (byte strings) eliminates conversion overhead and significantly speeds things up.b" ".join(result).decode(): By joining the list of byte strings withb" "and decoding only at the end, output is also performed efficiently.Using the
prevvariable: Instead of referencing the list tail withresult[-1]each time, holding the previous value in a separate variableprevmakes the code simpler (the complexity doesn’t change, but readability improves).Source Code
import sys
def main():
input_data = sys.stdin.buffer.read().split()
n = int(input_data[0])
a = input_data[1:n+1]
result = []
prev = None
for x in a:
if x != prev:
result.append(x)
prev = x
sys.stdout.write(b" ".join(result).decode() + "\n")
main()
This editorial was generated by claude4.6opus-thinking.
投稿日時:
最終更新: