結果

問題 No.2438 Double Least Square
ユーザー abap34abap34
提出日時 2023-08-13 02:05:27
言語 Julia
(1.10.2)
結果
AC  
実行時間 1,991 ms / 2,000 ms
コード長 2,591 bytes
コンパイル時間 227 ms
コンパイル使用メモリ 7,200 KB
実行使用メモリ 343,528 KB
最終ジャッジ日時 2024-04-09 14:30:59
合計ジャッジ時間 64,427 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,680 ms
310,944 KB
testcase_01 AC 1,709 ms
310,612 KB
testcase_02 AC 1,686 ms
311,284 KB
testcase_03 AC 1,672 ms
312,012 KB
testcase_04 AC 1,676 ms
310,796 KB
testcase_05 AC 1,691 ms
311,044 KB
testcase_06 AC 1,712 ms
311,168 KB
testcase_07 AC 1,728 ms
310,776 KB
testcase_08 AC 1,719 ms
310,012 KB
testcase_09 AC 1,704 ms
309,232 KB
testcase_10 AC 1,953 ms
339,656 KB
testcase_11 AC 1,961 ms
339,524 KB
testcase_12 AC 1,959 ms
335,516 KB
testcase_13 AC 1,963 ms
337,320 KB
testcase_14 AC 1,964 ms
339,212 KB
testcase_15 AC 1,970 ms
340,920 KB
testcase_16 AC 1,965 ms
343,528 KB
testcase_17 AC 1,991 ms
337,732 KB
testcase_18 AC 1,982 ms
339,476 KB
testcase_19 AC 1,985 ms
339,748 KB
testcase_20 AC 1,959 ms
338,908 KB
testcase_21 AC 1,935 ms
339,696 KB
testcase_22 AC 1,933 ms
337,492 KB
testcase_23 AC 1,958 ms
339,100 KB
testcase_24 AC 1,965 ms
337,380 KB
testcase_25 AC 1,949 ms
337,708 KB
testcase_26 AC 1,942 ms
342,288 KB
testcase_27 AC 1,955 ms
334,804 KB
testcase_28 AC 1,980 ms
336,996 KB
testcase_29 AC 1,978 ms
337,328 KB
testcase_30 AC 1,705 ms
310,368 KB
testcase_31 AC 1,705 ms
310,468 KB
testcase_32 AC 1,697 ms
311,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using Printf

function score(xy, x², y²)
    if x² == 0
        return 0
    end
    coef = xy / x²
    return y² + (coef^2 * x²) - (2coef * xy)
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, rev=true)

    # 全部lower
    xy = sum(x .* y)
    x² = sum(x .* x)
    y² = sum(y .* y)

    L_best = score(xy, x², y²)
    
    for i in eachindex(x)
        upper = slopes_perm[begin:i]    
        lower = slopes_perm[i+1:end]

        upper_set = Set(upper)

        upper_x = getindex.(Ref(x), upper)
        upper_y = getindex.(Ref(y .- H), upper)
        
        upper_xy = sum(upper_x .* upper_y)
        upper_x² = sum(upper_x .* upper_x)
        upper_y² = sum(upper_y .* upper_y)
        
        lower_x = getindex.(Ref(x), lower)
        lower_y = getindex.(Ref(y), lower)
        
        lower_xy = sum(lower_x .* lower_y)
        lower_x² = sum(lower_x .* lower_x)
        lower_y² = sum(lower_y .* lower_y)

        L_u = score(upper_xy, upper_x², upper_y²)
        L_b = score(lower_xy, lower_x², lower_y²)

        L = L_u + L_b
        if L < L_best
            L_best = L
        end
        
        for j in eachindex(x)
            new_idx = x_perm[j]     
            if new_idx in upper_set
                upper_xy -= x[new_idx] * (y[new_idx] - H)
                upper_x² -= x[new_idx]^2
                upper_y² -= (y[new_idx] - H)^2
                
                lower_xy += x[new_idx] * y[new_idx]
                lower_x² += x[new_idx]^2
                lower_y² += y[new_idx]^2
            else
                upper_xy += x[new_idx] * (y[new_idx] - H)
                upper_x² += x[new_idx]^2
                upper_y² += (y[new_idx] - H)^2

                lower_xy -= x[new_idx] * y[new_idx]
                lower_x² -= x[new_idx]^2
                lower_y² -= y[new_idx]^2
            end

            L_u = score(upper_xy, upper_x², upper_y²)
            L_b = score(lower_xy, lower_x², lower_y²)
    
            L = L_u + L_b
            if L < L_best
                L_best = L
            end
            
        end        
    end 
    @printf "%0.20f\n" L_best
end

main()
0