結果

問題 No.837 Noelちゃんと星々2
ユーザー FromBooskaFromBooska
提出日時 2023-03-18 19:00:53
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,837 bytes
コンパイル時間 216 ms
コンパイル使用メモリ 81,824 KB
実行使用メモリ 114,224 KB
最終ジャッジ日時 2023-10-18 17:27:39
合計ジャッジ時間 3,383 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

# これが1種類にまとめるのであればmedian
# ということはYをソートして、前から何個目までを1グループ目とするかで探索
# 1グループ目は1グループ目のmedianに、2グループ目は2グループ目のmedianに
# その最低値が答えか
# 二重ループでTLE
# 三分探索で最低値を探す、失敗
# 公式解説の1行目に二重ループを回避する計算方法がある
# Nが偶数であれば、距離=Yの大きいほうから半分の和-Yの小さいほうから半分の和
# Nが奇数であれば、Y[median]を飛ばして、距離=Yの大きいほうから半分の和-Yの小さいほうから半分の和
# なるほど!

from math import floor, ceil
N = int(input())
Y = list(map(int, input().split()))
Y.sort()
Y_cumu_front = [0]
temp = 0
for i in range(N):
    temp += Y[i]
    Y_cumu_front.append(temp)

if len(set(Y)) == 1:
    print(1)
    exit()

ans = 10**10
from math import floor, ceil
for group1_len in range(1, N):
    group2_len = N - group1_len
    cost = 0
    # なんとmedianを計算する必要がない
    if group1_len%2 == 1:
        cost += Y_cumu_front[group1_len]-Y_cumu_front[group1_len//2+1]
        cost -= Y_cumu_front[group1_len//2]-Y_cumu_front[0]
    else:
        cost += Y_cumu_front[group1_len]-Y_cumu_front[group1_len//2]
        cost -= Y_cumu_front[group1_len//2]-Y_cumu_front[0]
    if group2_len%2 == 1:
        cost += Y_cumu_front[N]-Y_cumu_front[group1_len+group2_len//2+1]
        cost -= Y_cumu_front[group1_len+group2_len//2]-Y_cumu_front[group1_len]
    else:
        cost += Y_cumu_front[N]-Y_cumu_front[group1_len+group2_len//2]
        cost -= Y_cumu_front[group1_len+group2_len//2]-Y_cumu_front[group1_len]
    ans = min(ans, cost)
    
    #print(group1_len, group2_len, cost, ans)

print(ans)



0