公式

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

  1. Prepare a list result to store the results, and a variable prev to hold the most recently added element (initialize prev to None to represent “nothing has been added yet”).
  2. Examine \(A_1, A_2, \ldots, A_N\) in order.
    • If the current event code \(A_i\) is different from prev, add it to result and update prev to \(A_i\).
    • If they are the same, do nothing (skip it).
  3. Output the contents of result separated 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 with split() is fast. In the solution code, event codes are compared and concatenated as byte strings (without converting to int). 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 with b" " and decoding only at the end, output is also performed efficiently.

  • Using the prev variable: Instead of referencing the list tail with result[-1] each time, holding the previous value in a separate variable prev makes 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.

投稿日時:
最終更新: