結果

問題 No.2248 max(C)-min(C)
ユーザー FromBooskaFromBooska
提出日時 2023-05-03 14:23:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 787 ms / 3,000 ms
コード長 1,649 bytes
コンパイル時間 415 ms
コンパイル使用メモリ 81,912 KB
実行使用メモリ 133,820 KB
最終ジャッジ日時 2024-05-01 13:42:27
合計ジャッジ時間 22,802 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,736 KB
testcase_01 AC 41 ms
52,736 KB
testcase_02 AC 40 ms
52,224 KB
testcase_03 AC 74 ms
67,712 KB
testcase_04 AC 69 ms
67,072 KB
testcase_05 AC 58 ms
64,128 KB
testcase_06 AC 77 ms
72,448 KB
testcase_07 AC 81 ms
72,448 KB
testcase_08 AC 79 ms
72,960 KB
testcase_09 AC 83 ms
73,344 KB
testcase_10 AC 58 ms
63,616 KB
testcase_11 AC 77 ms
72,448 KB
testcase_12 AC 70 ms
68,736 KB
testcase_13 AC 73 ms
71,936 KB
testcase_14 AC 78 ms
74,240 KB
testcase_15 AC 78 ms
73,600 KB
testcase_16 AC 64 ms
66,944 KB
testcase_17 AC 77 ms
73,216 KB
testcase_18 AC 549 ms
110,500 KB
testcase_19 AC 126 ms
81,820 KB
testcase_20 AC 703 ms
133,420 KB
testcase_21 AC 720 ms
113,152 KB
testcase_22 AC 446 ms
106,608 KB
testcase_23 AC 349 ms
98,688 KB
testcase_24 AC 167 ms
84,608 KB
testcase_25 AC 576 ms
110,104 KB
testcase_26 AC 567 ms
109,280 KB
testcase_27 AC 625 ms
109,856 KB
testcase_28 AC 122 ms
80,256 KB
testcase_29 AC 607 ms
111,544 KB
testcase_30 AC 256 ms
94,848 KB
testcase_31 AC 697 ms
133,548 KB
testcase_32 AC 184 ms
86,652 KB
testcase_33 AC 242 ms
92,544 KB
testcase_34 AC 706 ms
133,820 KB
testcase_35 AC 787 ms
133,672 KB
testcase_36 AC 676 ms
133,628 KB
testcase_37 AC 430 ms
103,812 KB
testcase_38 AC 647 ms
114,464 KB
testcase_39 AC 631 ms
114,332 KB
testcase_40 AC 638 ms
114,320 KB
testcase_41 AC 638 ms
112,220 KB
testcase_42 AC 636 ms
114,468 KB
testcase_43 AC 597 ms
111,872 KB
testcase_44 AC 717 ms
114,076 KB
testcase_45 AC 569 ms
112,292 KB
testcase_46 AC 296 ms
100,228 KB
testcase_47 AC 683 ms
114,524 KB
testcase_48 AC 533 ms
114,668 KB
testcase_49 AC 518 ms
132,904 KB
testcase_50 AC 525 ms
132,892 KB
testcase_51 AC 519 ms
132,940 KB
testcase_52 AC 520 ms
132,876 KB
testcase_53 AC 136 ms
84,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# Ciは3項の選択がある
# ある値を決め打って、一番近いmin、一番近いmaxを探したらどうか
# 三分探索で最小値探索

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

C = []
for i in range(N):
    temp = []
    temp.append(A[i])
    temp.append(B[i])
    temp.append((A[i]+B[i])//2)
    temp.sort()
    C.append(temp)
    
def check(X):
    mn = 10**10
    mx = -1
    for i in range(N):
        temp = C[i]
        if X <= (temp[0]+temp[1])/2:
            mn = min(mn, temp[0])
            mx = max(mx, temp[0])
        elif (temp[1]+temp[2])/2 <= X:
            mn = min(mn, temp[2])
            mx = max(mx, temp[2])        
        else:
            mn = min(mn, temp[1])
            mx = max(mx, temp[1]) 
    return (mx-mn) 

#for x in range(1, 20):
#    print(x, check(x))
    
# 谷、つまり1極値を求める三分探索での解き方
# https://roiti46.hatenablog.com/entry/2015/04/29/yukicoder_No.198_%E3%82%AD%E3%83%A3%E3%83%B3%E3%83%87%E3%82%A3%E3%83%BC%E3%83%BB%E3%83%9C%E3%83%83%E3%82%AF%E3%82%B9%EF%BC%92
# https://qiita.com/ganyariya/items/1553ff2bf8d6d7789127

# 範囲に注意、ギリギリありえない値とする
left, right = -1, 10**9+2
while right-left > 2:
    left_mid, right_mid = (2*left+right)//3, (left+2*right)//3
    if check(left_mid) <= check(right_mid):
        # 右midの方が大きいから右を右midに変える
        right = right_mid
    else:
        left = left_mid

#print(left_mid, right_mid)

ans = min(check(left_mid), check(right_mid))
# leftとrightの間も入れる必要あるのか? ない
print(ans)


0