結果

問題 No.1071 ベホマラー
ユーザー FromBooskaFromBooska
提出日時 2023-04-18 12:22:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 1,365 bytes
コンパイル時間 1,023 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 91,520 KB
最終ジャッジ日時 2024-04-21 12:18:52
合計ジャッジ時間 4,071 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
52,096 KB
testcase_01 AC 36 ms
52,480 KB
testcase_02 AC 36 ms
51,712 KB
testcase_03 AC 30 ms
52,096 KB
testcase_04 AC 31 ms
52,336 KB
testcase_05 AC 32 ms
51,968 KB
testcase_06 AC 31 ms
52,224 KB
testcase_07 AC 33 ms
57,856 KB
testcase_08 AC 33 ms
58,368 KB
testcase_09 AC 30 ms
52,224 KB
testcase_10 AC 122 ms
88,576 KB
testcase_11 AC 154 ms
87,220 KB
testcase_12 AC 76 ms
82,884 KB
testcase_13 AC 165 ms
86,144 KB
testcase_14 AC 92 ms
88,872 KB
testcase_15 AC 125 ms
91,364 KB
testcase_16 AC 112 ms
91,520 KB
testcase_17 AC 165 ms
91,008 KB
testcase_18 AC 122 ms
91,064 KB
testcase_19 AC 206 ms
91,136 KB
testcase_20 AC 87 ms
85,888 KB
testcase_21 AC 96 ms
91,136 KB
testcase_22 AC 89 ms
85,888 KB
testcase_23 AC 95 ms
91,212 KB
testcase_24 AC 116 ms
90,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# ベホイミをa回、ベホマラーをb回使うとする、そのときのコストaX+bYを最小化したい
# bを決めればaは決まる
# ということはコストはbの関数となる、まずそれを実装しよう

N, K, X, Y = map(int, input().split())
A = list(map(int, input().split()))
A = [t-1 for t in A]

def find_a(b):
    a = 0
    for i in range(N):
        calc = max(0, (A[i]-b*K+K-1)//K)
        a += calc
    return a

maxA = max(A)
b_max = (maxA+K-1)//K

def cost(b):
    a = find_a(b)
    return a*X+b*Y
    

if b_max == 0:
    print(cost(0))
    exit()


# 谷、つまり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, b_max+1
while right-left > 2:
    left_mid, right_mid = (2*left+right)//3, (left+2*right)//3
    if cost(left_mid) <= cost(right_mid):
        # 右midの方が大きいから右を右midに変える
        right = right_mid
    else:
        left = left_mid

#print(left_mid, right_mid)

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


0