結果
問題 | No.1008 Bench Craftsman |
ユーザー | tktk_snsn |
提出日時 | 2020-12-19 16:09:13 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 571 ms / 2,000 ms |
コード長 | 1,469 bytes |
コンパイル時間 | 257 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 189,532 KB |
最終ジャッジ日時 | 2024-09-21 10:23:10 |
合計ジャッジ時間 | 11,854 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
from itertools import accumulate import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) N, M = map(int, input().split()) A = tuple(map(int, input().split())) XW = tuple(tuple(map(int, input().split())) for _ in range(M)) X, W = zip(*XW) wsum = sum(W) if all(a > wsum for a in A): print(0) exit() def C(k): dpR = [0] * N dpL = [0] * N for x, w in XW: x -= 1 d = (w + k - 1) // k if x + 1 < N: dpR[x + 1] -= k if x + d < N: dpR[x + d] += k if N - 1 - (x - 1) < N: dpL[N - 1 - (x - 1)] -= k if N - 1 - (x - d) < N: dpL[N - 1 - (x - d)] += k dpR = list(accumulate(dpR)) dpL = list(accumulate(dpL)) for x, w in XW: x -= 1 d = (w + k - 1) // k dpR[x] += w dpL[N - 1 - x] += w if x + d < N: dpR[x + d] -= w dpR[x + d] += k * (d - 1) if N - 1 - (x - d) < N: dpL[N - 1 - (x - d)] -= w dpL[N - 1 - (x - d)] += k * (d - 1) dpR = list(accumulate(dpR)) dpL = list(accumulate(dpL)) dpL.reverse() B = [l + r for l, r in zip(dpL, dpR)] for x, w in XW: B[x - 1] -= w return all(b < a for a, b in zip(A, B)) inf = max(W) + 10 l = 0 r = inf while r - l > 1: m = (r + l) // 2 if C(m): r = m else: l = m # print(l, r) if r >= inf: print(-1) else: print(r)