結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー 👑 KazunKazun
提出日時 2021-02-28 05:01:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 1,125 bytes
コンパイル時間 126 ms
コンパイル使用メモリ 82,520 KB
実行使用メモリ 109,656 KB
最終ジャッジ日時 2024-04-10 15:56:13
合計ジャッジ時間 17,140 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,620 KB
testcase_01 AC 32 ms
52,996 KB
testcase_02 AC 33 ms
52,744 KB
testcase_03 AC 35 ms
53,408 KB
testcase_04 AC 35 ms
52,240 KB
testcase_05 AC 36 ms
52,200 KB
testcase_06 AC 35 ms
52,200 KB
testcase_07 AC 34 ms
53,240 KB
testcase_08 AC 39 ms
59,996 KB
testcase_09 AC 36 ms
58,504 KB
testcase_10 AC 33 ms
52,924 KB
testcase_11 AC 31 ms
52,792 KB
testcase_12 AC 33 ms
58,456 KB
testcase_13 AC 35 ms
58,844 KB
testcase_14 AC 39 ms
59,140 KB
testcase_15 AC 39 ms
59,520 KB
testcase_16 AC 39 ms
59,152 KB
testcase_17 AC 40 ms
58,796 KB
testcase_18 AC 297 ms
109,028 KB
testcase_19 AC 270 ms
104,680 KB
testcase_20 AC 182 ms
103,520 KB
testcase_21 AC 56 ms
66,568 KB
testcase_22 AC 185 ms
104,236 KB
testcase_23 AC 51 ms
66,008 KB
testcase_24 AC 239 ms
103,212 KB
testcase_25 AC 182 ms
103,664 KB
testcase_26 AC 92 ms
77,544 KB
testcase_27 AC 52 ms
66,816 KB
testcase_28 AC 315 ms
109,584 KB
testcase_29 AC 285 ms
109,656 KB
testcase_30 AC 300 ms
109,568 KB
testcase_31 AC 312 ms
109,148 KB
testcase_32 AC 284 ms
109,432 KB
testcase_33 AC 289 ms
109,444 KB
testcase_34 AC 304 ms
109,180 KB
testcase_35 AC 305 ms
108,796 KB
testcase_36 AC 296 ms
109,024 KB
testcase_37 AC 279 ms
109,316 KB
testcase_38 AC 284 ms
109,196 KB
testcase_39 AC 311 ms
108,944 KB
testcase_40 AC 321 ms
109,064 KB
testcase_41 AC 304 ms
109,556 KB
testcase_42 AC 294 ms
109,064 KB
testcase_43 AC 32 ms
52,800 KB
testcase_44 AC 32 ms
53,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Ternary_Search(L,R,f,Integer=True,arg=False,ep=1/(1<<20),Times=50):
    
    if Integer:
        while (R-L)>3:
            a=(2*L+R)//3
            b=(L+2*R)//3
            p=f(a);q=f(b)
            if p<=q:
                R=b
            else:
                L=a

        a=(2*L+R)//3
        b=(L+2*R)//3
    else:
        while (R-L)>=ep and Times:
            Times-=1
            a=(2*L+R)/3
            b=(L+2*R)/3

            p=f(a);q=f(b)
            if p<=q:
                R=b
            else:
                L=a

        a=(2*L+R)/3
        b=(L+2*R)/3

    if arg:
        y=float("inf")
        argx=-1
        for x in [L,a,b,R]:
            p=f(x)
            if y>p:
                y=p
                argx=x
        return y,argx
    else:
        return min(f(L),f(a),f(b),f(R))
#================================================
def f(x):
    X=0
    for a,b in zip(A,B):
        X+=b*abs(x-a)
    return X
#================================================
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

y,x=Ternary_Search(min(A),max(A),f,True,True)
print(x,y)
0