結果

問題 No.275 中央値を求めよ
ユーザー tanson
提出日時 2025-07-12 02:01:49
言語 Standard ML
(MLton 20210117)
結果
AC  
実行時間 6 ms / 1,000 ms
コード長 947 bytes
コンパイル時間 5,878 ms
コンパイル使用メモリ 687,564 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-07-12 02:01:58
合計ジャッジ時間 7,771 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

fun readInt () =
    valOf (TextIO.scanStream (Int.scan StringCvt.DEC) TextIO.stdIn)

fun realString n =
    if 0.0 <= n
    then Real.toString n
    else "-" ^ Real.toString (Real.abs n)

fun insertionSort l =
    let
        fun insert x [] = [x]
          | insert x (y::ys) = if x <= y then x::y::ys
                               else y::(insert x ys)
    in
        foldl (fn (x, acc) => insert x acc) [] l
    end

val () =
    let
        val n = readInt ()
        val a_s = List.tabulate (n, fn _ => readInt ())
        fun findMedian l =
            let
                val sorted = insertionSort l
                val i = length l div 2
            in
                if length l mod 2 = 0
                then (Real.fromInt (List.nth(sorted, (i - 1)) + List.nth(sorted, i) )) / 2.0
                else Real.fromInt (List.nth(sorted, i))
            end

        val ans = findMedian a_s
    in
        print (realString ans)
    end
0