結果

問題 No.2438 Double Least Square
ユーザー abap34abap34
提出日時 2023-08-17 22:25:59
言語 Julia
(1.10.2)
結果
AC  
実行時間 1,935 ms / 2,000 ms
コード長 2,230 bytes
コンパイル時間 43 ms
コンパイル使用メモリ 6,816 KB
実行使用メモリ 342,124 KB
最終ジャッジ日時 2024-04-09 14:32:38
合計ジャッジ時間 61,648 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,631 ms
309,944 KB
testcase_01 AC 1,654 ms
309,408 KB
testcase_02 AC 1,609 ms
309,740 KB
testcase_03 AC 1,621 ms
311,248 KB
testcase_04 AC 1,646 ms
309,820 KB
testcase_05 AC 1,636 ms
309,784 KB
testcase_06 AC 1,644 ms
309,764 KB
testcase_07 AC 1,630 ms
311,068 KB
testcase_08 AC 1,631 ms
310,164 KB
testcase_09 AC 1,641 ms
311,644 KB
testcase_10 AC 1,871 ms
341,260 KB
testcase_11 AC 1,934 ms
340,816 KB
testcase_12 AC 1,889 ms
337,916 KB
testcase_13 AC 1,864 ms
336,960 KB
testcase_14 AC 1,867 ms
338,768 KB
testcase_15 AC 1,895 ms
337,852 KB
testcase_16 AC 1,855 ms
341,120 KB
testcase_17 AC 1,868 ms
338,764 KB
testcase_18 AC 1,868 ms
337,140 KB
testcase_19 AC 1,859 ms
337,344 KB
testcase_20 AC 1,830 ms
336,788 KB
testcase_21 AC 1,835 ms
342,124 KB
testcase_22 AC 1,825 ms
337,616 KB
testcase_23 AC 1,889 ms
339,028 KB
testcase_24 AC 1,851 ms
339,036 KB
testcase_25 AC 1,861 ms
338,436 KB
testcase_26 AC 1,889 ms
336,728 KB
testcase_27 AC 1,928 ms
339,188 KB
testcase_28 AC 1,935 ms
340,768 KB
testcase_29 AC 1,917 ms
336,772 KB
testcase_30 AC 1,630 ms
309,596 KB
testcase_31 AC 1,636 ms
309,212 KB
testcase_32 AC 1,641 ms
309,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using Printf

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

    slopes = (y .- (H // 2)) .// x

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

    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 = x[upper]
        upper_y = y[upper] .- H

        upper_xy = sum(upper_x .* upper_y)
        upper_x² = sum(upper_x .* upper_x)
        upper_y² = sum(upper_y .* upper_y)
        
        lower_x = x[lower]
        lower_y = 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]     
            _x = x[new_idx]
            _y = y[new_idx] 
            if new_idx in upper_set
                upper_xy -= _x * (_y - H)
                upper_x² -= _x^2
                upper_y² -= (_y - H)^2
                
                lower_xy += _x * _y
                lower_x² += _x^2
                lower_y² += _y^2
            else
                upper_xy += _x * (_y - H)
                upper_x² += _x^2
                upper_y² += (_y - H)^2

                lower_xy -= _x * _y
                lower_x² -= _x^2
                lower_y² -= _y^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