結果

問題 No.871 かえるのうた
コンテスト
ユーザー Theta
提出日時 2022-11-25 14:46:16
言語 Python3
(3.15.0 + numpy 2.5.4 + scipy 1.18.1 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
WJ  
(最新)
AC  
(最初)
実行時間 -
コード長 1,532 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
最終ジャッジ日時 2026-10-13 23:30:35
ジャッジサーバーID
(参考情報)
judge1_0 / judge5_0
このコードへのチャレンジ
(要ログイン)

ソースコード

diff #
raw source code

from bisect import bisect_left, bisect_right
from math import inf
from typing import NamedTuple


class Frog(NamedTuple):
    coord: int
    amp: int

    def is_range(self, start: int, end: int) -> bool:
        return start <= self.coord <= end

    @property
    def range(self) -> tuple[int, int]:
        return self.coord-self.amp, self.coord+self.amp


def main():
    N, K = map(int, input().split())
    X = list(map(int, input().split()))
    A = list(map(int, input().split()))
    frogs: list[Frog] = [Frog(coord, amp) for coord, amp in zip(X, A)]

    left_effect = X[K-1]-A[K-1]
    right_effect = X[K-1]+A[K-1]
    left_idx = K-1
    right_idx = K
    idx_changed = True
    
    while idx_changed:
        new_gelo: list[Frog] = []
        idx_changed = False

        left_idx_candidate = bisect_left(X, left_effect)
        right_idx_candidate = bisect_right(X, right_effect)

        if left_idx != left_idx_candidate:
            idx_changed = True
            new_gelo.extend(frogs[left_idx_candidate:left_idx])
        left_idx = left_idx_candidate

        if right_idx != right_idx_candidate:
            idx_changed = True
            new_gelo.extend(frogs[right_idx:right_idx_candidate])
            right_idx = right_idx_candidate

        for frog in new_gelo:
            left_range, right_range = frog.range
            left_effect = min(left_effect, left_range)
            right_effect = max(right_effect, right_range)
        
    print(right_idx - left_idx)


if __name__ == "__main__":
    main()
0