結果

問題 No.1150 シュークリームゲーム(Easy)
ユーザー qwewe
提出日時 2025-05-14 12:47:23
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,617 bytes
コンパイル時間 4,434 ms
コンパイル使用メモリ 82,108 KB
実行使用メモリ 106,184 KB
最終ジャッジ日時 2025-05-14 12:47:46
合計ジャッジ時間 4,995 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 2 WA * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1
    s = int(input[ptr])
    t = int(input[ptr + 1])
    ptr += 2
    A = list(map(int, input[ptr:ptr + N]))
    ptr += N

    adjacent = False
    if abs(s - t) == 1 or (s == 1 and t == N) or (s == N and t == 1):
        adjacent = True

    if adjacent:
        start = (s % N) + 1
        end = t - 1 if t > 1 else N

        remaining_vertices = []
        if start <= end:
            remaining_vertices = list(range(start, end + 1))
        else:
            part1 = list(range(start, N + 1))
            part2 = list(range(1, end + 1))
            remaining_vertices = part1 + part2

        dq = deque([A[v - 1] for v in remaining_vertices])

        x = 0
        y = 0
        turn = 0

        while dq:
            if turn == 0:
                val = dq.popleft()
                x += val
            else:
                val = dq.pop()
                y += val
            turn = 1 - turn

        X = x + A[s - 1]
        Y = y + A[t - 1]
        result = X - Y
    else:
        def get_clock_path(s, t, N):
            path = []
            current = s
            while True:
                current = current % N + 1
                if current == t:
                    break
                path.append(current)
            return path

        def get_counter_clock_path(s, t, N):
            path = []
            current = s
            while True:
                current = (current - 2) % N + 1
                if current == t:
                    break
                path.append(current)
            return path

        path_clock = get_clock_path(s, t, N)
        path_counter_clock = get_counter_clock_path(s, t, N)

        dq_a = deque([A[v - 1] for v in path_clock])
        dq_b = deque([A[v - 1] for v in path_counter_clock])

        x = 0
        y = 0
        turn = 0

        while dq_a or dq_b:
            a_val = dq_a[0] if dq_a else -1
            b_val = dq_b[0] if dq_b else -1

            if a_val >= b_val and a_val != -1:
                val = dq_a.popleft()
            elif b_val != -1:
                val = dq_b.popleft()
            else:
                if dq_a:
                    val = dq_a.popleft()
                else:
                    val = dq_b.popleft()

            if turn == 0:
                x += val
            else:
                y += val
            turn = 1 - turn

        X = x + A[s - 1]
        Y = y + A[t - 1]
        result = X - Y

    print(result)

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