公式

A - 待ち合わせ場所 / Meeting Place 解説 by admin

gpt-5.3-codex

Overview

This is a problem where you simply extract the coordinate \(X_K\) of the \(K\)-th building and output \(X_K - 1\) as the meeting point.
Since all you need is “the \(K\)-th value,” there is no need to process the entire input in a complex way.

Analysis

The key observation is the following single point:

  • The desired answer is \(X_K - 1\), and among \(X_1, X_2, \dots, X_N\), only the \(K\)-th value is needed.

For example,
if \(N=5, K=3\) and the coordinates are \(10, -4, 7, 100, 0\), then we only need \(X_3=7\), and the answer is \(7-1=6\).


As a straightforward implementation, storing all coordinates in an array and then outputting X[K-1]-1 will give the correct answer.
However, for this problem, even storing an array is unnecessary. It suffices to read the input line by line and remember the value only when \(i=K\).

This approach keeps memory usage low while also keeping the logic simple.

Algorithm

  1. Read \(N, K\).
  2. Read coordinates \(x\) sequentially for \(i=1\) through \(N\).
  3. If \(i=K\), save that value in xk.
  4. Finally, output xk - 1.

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(1)\)

Implementation Notes

  • Since building numbers are 1-indexed, using for i in range(1, N+1) for the loop makes it less error-prone.

  • Coordinates range from \(-10^{18}\) to \(10^{18}\), but Python’s int handles this without any issues.

  • Using sys.stdin.readline ensures stable and fast input reading even when the input is large.

    Source Code

import sys

def main():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    xk = None
    for i in range(1, N + 1):
        x = int(input().strip())
        if i == K:
            xk = x
    print(xk - 1)

if __name__ == "__main__":
    main()

This editorial was generated by gpt-5.3-codex.

投稿日時:
最終更新: