結果

問題 No.198 キャンディー・ボックス2
ユーザー FromBooskaFromBooska
提出日時 2023-10-17 12:59:02
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,231 bytes
コンパイル時間 915 ms
コンパイル使用メモリ 81,540 KB
実行使用メモリ 68,584 KB
最終ジャッジ日時 2023-10-17 12:59:06
合計ジャッジ時間 3,526 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,472 KB
testcase_01 AC 38 ms
53,472 KB
testcase_02 AC 38 ms
53,472 KB
testcase_03 AC 37 ms
53,472 KB
testcase_04 AC 38 ms
53,472 KB
testcase_05 AC 38 ms
53,472 KB
testcase_06 AC 38 ms
53,472 KB
testcase_07 AC 37 ms
53,472 KB
testcase_08 AC 37 ms
53,472 KB
testcase_09 AC 37 ms
53,472 KB
testcase_10 RE -
testcase_11 AC 38 ms
53,476 KB
testcase_12 AC 37 ms
53,476 KB
testcase_13 AC 38 ms
53,476 KB
testcase_14 RE -
testcase_15 AC 38 ms
53,476 KB
testcase_16 RE -
testcase_17 AC 38 ms
53,476 KB
testcase_18 AC 39 ms
53,476 KB
testcase_19 AC 38 ms
53,476 KB
testcase_20 AC 38 ms
53,476 KB
testcase_21 AC 38 ms
53,476 KB
testcase_22 AC 38 ms
53,476 KB
testcase_23 AC 39 ms
53,476 KB
testcase_24 AC 39 ms
53,476 KB
testcase_25 AC 39 ms
53,476 KB
testcase_26 AC 38 ms
53,476 KB
testcase_27 AC 38 ms
53,476 KB
testcase_28 AC 38 ms
53,476 KB
testcase_29 AC 38 ms
53,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 二分探索に見えるが、単調ではなく2次曲線となるので三分探索

B = int(input())
N = int(input())
C = []
for i in range(N):
    C.append(int(input()))

# 三分探索ではreturn 0 or 1ではなくコストが必要
def check(X):
    count = 0
    for c in C:
        count += abs(c-X)
    if count <= X:
        return count
    else:
        return count
    
#for i in range(0, 10):
#    print(i, check(i))

# 谷、つまり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, (sum(C)+B)//N+1
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