結果
| 問題 |
No.1597 Matrix Sort
|
| コンテスト | |
| ユーザー |
norioc
|
| 提出日時 | 2024-06-13 09:25:50 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 943 ms / 1,500 ms |
| コード長 | 1,704 bytes |
| コンパイル時間 | 968 ms |
| コンパイル使用メモリ | 82,432 KB |
| 実行使用メモリ | 374,144 KB |
| 最終ジャッジ日時 | 2024-06-13 09:26:10 |
| 合計ジャッジ時間 | 19,398 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
from itertools import accumulate
from bisect import bisect_left, bisect_right
def find_interval(a: list, lo: int, hi: int) -> tuple[int, int, int]:
"""ソート済みリスト a の要素の lo 以上 hi 以下の個数と区間を返す
return: 範囲内の個数, l, r
"""
if lo > hi: return 0, -1, -1
assert lo <= hi
empty = 0, -1, -1 # 区間なし
if not a or lo > a[-1] or hi < a[0]: return empty
l = bisect_left(a, lo)
r = bisect_right(a, hi) - 1
if l > r: return empty
return r-l+1, l, r
def cumsum(seq, reverse=False) -> list:
if reverse:
res = cumsum(reversed(seq))
res.reverse()
return res
return list(accumulate(seq))
def accum(a: list):
acc = list(accumulate(a))
return lambda l, r: acc[r] - (acc[l-1] if l > 0 else 0)
N, K, P = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.sort()
B.sort()
bs = [0] * (P+1)
for b in B:
bs[b] += 1
acc_bs = accum(bs)
# A_i + B_j <= x を満たす (i, j) の個数を返す
def count(x: int) -> int:
res = 0
for a in A: # A_i を固定
# a + B_j <= x を満たす j の範囲
# cnt1, _, _ = find_interval(B, 0, x-a)
# a + B_j <= P + x を満たす j の範囲
# cnt2, _, _ = find_interval(B, P-a, P-a+x)
# res += cnt1 + cnt2
if 0 <= x-a:
res += acc_bs(0, x-a)
res += acc_bs(P-a, min(P, P-a+x))
return res
lo = 0
hi = P
ans = hi
while lo <= hi:
m = (lo + hi) // 2
cnt = count(m)
# print(f'{m=} {cnt=}')
if cnt >= K:
ans = min(ans, m)
hi = m - 1
else:
lo = m + 1
print(ans)
norioc