公式

B - 数直線を進む / Walking on the Number Line 解説 by admin

gpt-6-astra-medium

Overview

Starting from coordinate \(0\), this problem asks us to process movements to the right or left in order and output the coordinate immediately after each movement. We maintain the total distance moved to the right and the total distance moved to the left.

Analysis

On a number line, moving right increases the coordinate, and moving left decreases it. Therefore, the current coordinate can be calculated using the following formula:

\(\text{Current coordinate} = \text{Total distance moved to the right} - \text{Total distance moved to the left}\)

For example, if the movements are R 5, L 3, and L 4 in that order, the values change as follows:

Movement Total to the right Total to the left Coordinate after movement
R 5 5 0 5
L 3 5 3 2
L 4 5 7 -2

For this problem, a straightforward simulation that processes the movements in order is fast enough. There is no need to simulate moving distance \(1\) step by step; by adding the movement distance all at once, each movement can be processed in constant time.

Note that the problem asks for the signed coordinate, not the distance from the origin. If there are more movements to the left, the coordinate becomes negative, so taking the absolute value of the difference would result in an incorrect answer.

Algorithm

  1. Read the number of movements \(N\).
  2. Initialize both the total distance moved right right and the total distance moved left left to \(0\).
  3. For each movement, perform the following:
    • Read the direction \(D_i\) and distance \(X_i\).
    • If \(D_i\) is R, add \(X_i\) to right.
    • If \(D_i\) is L, add \(X_i\) to left.
    • Output right - left.

After processing each movement, right and left represent the total distances moved to the right and left up to that point, respectively. Since the initial coordinate is \(0\), outputting their difference gives the correct coordinate immediately after that movement.

Complexity

  • Time Complexity: \(O(N)\)
    Each movement is processed in constant time.
  • Space Complexity: \(O(1)\)
    Only a few variables for the totals are maintained, without storing the history of movements.

Implementation Details

  • Output the result after updating the totals. Outputting before updating will not give the coordinate immediately after the movement.

  • Do not output the initial coordinate \(0\); output the coordinates after each movement for a total of \(N\) lines.

  • From the constraints, the maximum absolute value of the coordinate is at most \(100 \times 1000 = 100000\), which easily fits in a standard 32-bit int in C++. The provided code uses long long.

  • Since the direction is only L or R, if d == 'R' is not satisfied, it can be treated as a movement to the left.

    Source Code

// 問題文から独立に実装: 右への合計と左への合計を別々に持つ
#include <iostream>
int main() {
    int n; std::cin >> n;
    long long right = 0, left = 0;
    while (n--) {
        char d; long long x; std::cin >> d >> x;
        if (d == 'R') right += x; else left += x;
        std::cout << right - left << "\n";
    }
}

This editorial was generated by gpt-6-astra-medium.