結果

問題 No.2438 Double Least Square
ユーザー abap34abap34
提出日時 2023-08-11 23:35:59
言語 Julia
(1.10.2)
結果
TLE  
実行時間 -
コード長 1,718 bytes
コンパイル時間 32 ms
コンパイル使用メモリ 6,684 KB
実行使用メモリ 964,712 KB
最終ジャッジ日時 2024-04-09 14:30:54
合計ジャッジ時間 6,742 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

using Printf

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


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



    # (0, W)からの傾きでsortする.
    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    
    hu = -1
    hd = -1

    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)

            coef_hu, l_hu = solve(x_hu, y_hu)

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

            coef_hd, l_hd = solve(x_hd, y_hd)
            
            L = l_hu + l_hd
            if L < L_best
                L_best = L
                hu = H + coef_hu * W
                hd = 0 + coef_hd * W
            end
        end
    end 


    @printf "%0.20f\n" L_best
end

main()
0