結果

問題 No.972 選び方のスコア
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-06-30 09:28:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 2,287 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 87,128 KB
実行使用メモリ 116,040 KB
最終ジャッジ日時 2023-09-09 03:13:54
合計ジャッジ時間 9,092 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 214 ms
115,692 KB
testcase_01 AC 217 ms
115,548 KB
testcase_02 AC 213 ms
115,180 KB
testcase_03 AC 141 ms
97,992 KB
testcase_04 AC 208 ms
114,580 KB
testcase_05 AC 211 ms
114,076 KB
testcase_06 AC 213 ms
114,252 KB
testcase_07 AC 174 ms
102,448 KB
testcase_08 AC 179 ms
114,180 KB
testcase_09 AC 180 ms
113,852 KB
testcase_10 AC 155 ms
113,924 KB
testcase_11 AC 154 ms
113,956 KB
testcase_12 AC 174 ms
114,100 KB
testcase_13 AC 164 ms
113,932 KB
testcase_14 AC 176 ms
114,252 KB
testcase_15 AC 196 ms
115,336 KB
testcase_16 AC 188 ms
116,040 KB
testcase_17 AC 186 ms
115,520 KB
testcase_18 AC 70 ms
71,320 KB
testcase_19 AC 210 ms
108,204 KB
testcase_20 AC 206 ms
107,572 KB
testcase_21 AC 210 ms
107,956 KB
testcase_22 AC 205 ms
114,448 KB
testcase_23 AC 212 ms
107,936 KB
testcase_24 AC 207 ms
107,716 KB
testcase_25 AC 67 ms
71,440 KB
testcase_26 AC 67 ms
71,448 KB
testcase_27 AC 68 ms
71,380 KB
testcase_28 AC 68 ms
71,136 KB
testcase_29 AC 68 ms
71,240 KB
testcase_30 AC 70 ms
71,556 KB
testcase_31 AC 70 ms
71,212 KB
testcase_32 AC 71 ms
71,404 KB
testcase_33 AC 74 ms
75,220 KB
testcase_34 AC 70 ms
71,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""
選択する数字は奇数個でよいか。
奇数個取る場合、中央値はaのいずれかになるので扱いやすい
偶数個取る場合、中央値の候補がn^2個あり扱いが難しい。

偶数個取るとする。
bの中央値をmとし、mを作る二つの要素をm-d,m+dとする。簡単のため要素はすべて異なるとする。
sum0=sum([x-m for x in b if x<=m-d])
sum1=sum([x-m for x in b if x>=m+d])
n0=len([x-m for x in b if x<=m-d])
n1=len([x-m for x in b if x>=m+d])
sum0+sum1がスコア

このときbから要素m+dを取り除くと中央値はm-dになる。
新しいスコアは
new_sum0=sum([x-(m-d) for x in b if x<=m-d])=sum([x-m for x in b if x<=m-d]) + d * n0 = sum0 + d * n0
new_sum1=sum([x-(m-d) for x in b if x>m+d])=sum([x-(m-d) for x in b if x>=m+d]) - (m+d-(m-d))
=sum([x-m for x in b if x>=m+d]) - 2*d + n1*d = sum1 + d * (n1-2)
->sum0+sum1+d*(n0+n1-2)
n0==n1,n0>0なのでn0+n1>=2.偶数個取った場合、真ん中の大きい方を取り除いた数列にしても損することはない。
よって奇数個取ることだけを考えてよい。
"""
def main0(n,a):
  # O(N^2)
  ans=0
  a.sort()
  for i in range(n):
    # a[i]を中央値とする。
    now=0
    # 貪欲に取っていく。要素を増やしたとき負が加算されるまで取り続ける。
    for j in range(n):
      if -n<=-i-j and 0<=i-1-j and (a[-1-j]-a[i]) + (a[i-1-j]-a[i])>=0:
        now+=a[-1-j]-a[i]
        now+=a[i-1-j]-a[i]
      else:
        break
    ans=max(ans,now)
  return ans

def main1(n,a):
  # main0の貪欲部分を二分探索する。
  ans=0
  a.sort()
  sa=[0]
  for x in a:sa.append(sa[-1]+x)
  for i in range(n):
    # a[i]を中央値とする。
    # 貪欲に取っていく。二分探索。2x+1個の要素からなる数列。xを求める。x=0なら0。x>=1の範囲を探索
    l,r=1,min(i,n-i-1)+1
    while r-l>1:
      x=(r+l)//2
      if (a[-x]-a[i])+(a[i-x]-a[i])>=0:
        l,r=x,r
      else:
        l,r=l,x
    # x=l+1
    x=l
    #print(i,x,sa)
    tmp=sa[n]-sa[n-x] + sa[i]-sa[i-x]
    tmp-=a[i]*(2*x)
    ans=max(ans,tmp)
  return ans

if __name__=='__main__':
  n=int(input())
  a=list(map(int,input().split()))
  ret1=main1(n,a)
  print(ret1)
  #ret0=main0(n,a)
  #print(ret0)
0