結果

問題 No.1715 Dinner 2
ユーザー LyricalMaestroLyricalMaestro
提出日時 2024-01-08 01:37:55
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,976 bytes
コンパイル時間 215 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 170,864 KB
最終ジャッジ日時 2024-01-08 01:38:00
合計ジャッジ時間 5,583 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
62,404 KB
testcase_01 AC 46 ms
61,016 KB
testcase_02 AC 66 ms
70,572 KB
testcase_03 AC 42 ms
55,600 KB
testcase_04 AC 59 ms
68,484 KB
testcase_05 AC 60 ms
68,488 KB
testcase_06 AC 43 ms
55,600 KB
testcase_07 AC 57 ms
66,408 KB
testcase_08 AC 58 ms
68,480 KB
testcase_09 AC 46 ms
61,016 KB
testcase_10 AC 57 ms
66,248 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/1715

from collections import deque

def check(N, D, pq, border):
    # 料理に対するborderを求める
    borders = []
    for i, (p, q) in enumerate(pq):
        borders.append((border + p, i))
    borders.sort(reverse=True, key=lambda x: x[0])

    dp = [[-float("inf")] * (N + 1) for _ in range(D + 1)]
    dp[0][0] = 0
    for d in range(1, D + 1):
        array = [(i, dp[d - 1][i]) for i in range(N + 1)]
        array.sort(reverse=True, key=lambda x: x[1])

        ind = 0
        queue = deque()
        for b, i in borders:
            while ind < N + 1 and array[ind][1] >= b:
                if len(queue) == 0 or array[ind][1] > queue[0][1]:
                    queue.appendleft(array[ind])
                else:
                    while len(queue) > 1 and queue[-1][1] < array[ind][1]:
                        queue.pop()
                    queue.append(array[ind])
                ind += 1                    

            if len(queue) == 0:
                continue
            elif len(queue) == 1:
                if queue[0][0] != i + 1:
                    dp[d][i + 1] = queue[0][1] + pq[i][1] - pq[i][0]
            else:
                if queue[0][0] != i + 1:
                    dp[d][i + 1] = queue[0][1] + pq[i][1] - pq[i][0]
                else:
                    dp[d][i + 1] = queue[1][1] + pq[i][1] - pq[i][0]

    ans = -float("inf")
    for i in range(N + 1):
        ans = max(dp[D][i], ans)
    return ans != - float("inf")


def main():
    N, D = map(int, input().split())
    pq = []
    for _ in range(N):
        p, q = map(int, input().split())
        pq.append((p, q))
    
    low = -10 ** 9
    high = 10 ** 9
    while high - low > 1:
        mid = (low + high) // 2
        if check(N, D, pq, mid):
            low = mid
        else:
            high = mid

    if check(N, D, pq, high):
        print(high)
    else:
        print(low)




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