結果

問題 No.1460 Max of Min
ユーザー lam6er
提出日時 2025-03-31 17:30:13
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,229 bytes
コンパイル時間 358 ms
コンパイル使用メモリ 83,072 KB
実行使用メモリ 742,232 KB
最終ジャッジ日時 2025-03-31 17:31:21
合計ジャッジ時間 7,503 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 6 TLE * 1 MLE * 1 -- * 83
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    K = int(input[idx]); idx += 1
    N = int(input[idx]); idx += 1
    A = list(map(int, input[idx:idx+K]))
    idx += K
    B = list(map(int, input[idx:idx+K]))

    if N < K:
        print(A[N])
        return

    current = A[:]  # Current state: the last K elements
    seen = {}
    step = K - 1  # We are about to compute step K
    seen[tuple(current)] = step

    while True:
        step += 1
        next_val = -float('inf')
        for j in range(K):
            prev_val = current[j]
            candidate = min(prev_val, B[j])
            if candidate > next_val:
                next_val = candidate
        # Update current state: remove the first element, append next_val
        current = current[1:] + [next_val]
        current_tuple = tuple(current)

        if current_tuple in seen:
            # Cycle detected
            cycle_start = seen[current_tuple]
            cycle_length = step - cycle_start
            if N <= step:
                print(current[-1])
                return
            remaining = N - cycle_start
            effective_remainder = remaining % cycle_length
            effective_step = cycle_start + effective_remainder
            # Simulate from cycle_start step forward by effective_remainder steps
            # We need to calculate the value at effective_step
            # Reset current to the state at cycle_start
            current = list(current_tuple)
            # The current in 'seen' is the one that leads to the cycle
            # Now simulate 'effective_remainder' steps
            for _ in range(effective_remainder):
                next_val = -float('inf')
                for j in range(K):
                    prev_val = current[j]
                    candidate = min(prev_val, B[j])
                    if candidate > next_val:
                        next_val = candidate
                current = current[1:] + [next_val]
            print(current[-1])
            return
        else:
            seen[current_tuple] = step

        # Check if we have reached N
        if step == N:
            print(current[-1])
            return

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