結果

問題 No.731 等差数列がだいすき
ユーザー maimai
提出日時 2018-09-07 21:41:46
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 716 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 27,292 KB
最終ジャッジ日時 2024-05-07 00:41:52
合計ジャッジ時間 6,366 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 174 ms
19,612 KB
testcase_01 WA -
testcase_02 AC 229 ms
12,544 KB
testcase_03 TLE -
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def ascan; gets.split.map(&:to_f); end


def trisearch(left, right, equal = nil, &block)
    
    100.times {
        l = right - (2 * right - 2 * left) / 3.0
        r = right - (right - left) / 3.0

        lval = block.call(l)
        rval = block.call(r)
        
        if (lval <= rval)
            right = r
        else
            left = l
        end
        
        return left if (left == right)
    }
    return left
end


N = gets.to_i
A = ascan

b = 0
d = 0

100.times do 
    b = trisearch(0.0,1e5){|x| N.times.map{|i| (A[i]-(x+d*i))**2}.reduce(:+)}
    d = trisearch(-1e5,1e5){|x| N.times.map{|i| (A[i]-(b+x*i))**2}.reduce(:+)}
end

puts "#{b} #{d}"
p N.times.map{|i| (A[i]-(b+d*i))**2}.reduce(:+)
0