結果

問題 No.2438 Double Least Square
ユーザー abap34abap34
提出日時 2023-08-12 00:27:34
言語 Julia
(1.10.2)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,540 bytes
コンパイル時間 38 ms
コンパイル使用メモリ 6,816 KB
実行使用メモリ 301,472 KB
最終ジャッジ日時 2024-04-09 14:31:11
合計ジャッジ時間 21,315 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,543 ms
299,040 KB
testcase_01 AC 1,548 ms
301,180 KB
testcase_02 AC 1,550 ms
301,148 KB
testcase_03 AC 1,528 ms
297,516 KB
testcase_04 AC 1,514 ms
299,476 KB
testcase_05 AC 1,525 ms
297,380 KB
testcase_06 AC 1,529 ms
299,084 KB
testcase_07 AC 1,536 ms
301,472 KB
testcase_08 AC 1,530 ms
301,432 KB
testcase_09 AC 1,533 ms
296,936 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

using Printf

function solve(x::AbstractArray{Int}, y::AbstractArray{Int})
    if isempty(x)
        return 0
    end
    coef = sum(x .* y) / sum(x -> x^2, x)
    l = sum(x -> x^2, (y .- x .* coef))
    return l
end


function main()
    N = parse(Int, readline())
    H = parse(Int, readline())
    x = zeros(Int, N)
    y = zeros(Int, N)
    for i in 1:N
        x[i], y[i] = parse.(Int, split(readline()))
    end



    INF = 10^16
    slopes = zeros(Rational{Int}, N)
    for i in eachindex(x)
        if x[i] == 0
            slopes[i] = INF 
        else
            slopes[i] = (y[i] - (H // 2)) // x[i]
        end
    end


    slopes_perm = sortperm(slopes, rev=true)
    x_perm = sortperm(x)

    L_best = Inf    

    for i in eachindex(x)
        upper = slopes_perm[begin:i]
        lower = slopes_perm[i+1:end]
        for j in eachindex(x)
            left = Set(x_perm[begin:j])
            right = Set(x_perm[j+1:end])
            
            target_hu = (left ∩ upper) ∪ (right ∩ lower)
            target_hd = (left ∩ lower) ∪ (right ∩ upper)
            
            x_hu = getindex.(Ref(x), target_hu)
            y_hu = getindex.(Ref(y .- H), target_hu)

            l_hu = solve(x_hu, y_hu)

            x_hd = getindex.(Ref(x), target_hd)
            y_hd = getindex.(Ref(y), target_hd)

            l_hd = solve(x_hd, y_hd)
            
            L = l_hu + l_hd

            if L < L_best
                L_best = L
            end
        end
    end 


    @printf "%0.20f\n" L_best
end

main()
0