結果
| 問題 |
No.409 ダイエット
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-04-19 00:04:05 |
| 言語 | Ruby (3.4.1) |
| 結果 |
AC
|
| 実行時間 | 562 ms / 2,000 ms |
| コード長 | 1,061 bytes |
| コンパイル時間 | 53 ms |
| コンパイル使用メモリ | 7,552 KB |
| 実行使用メモリ | 34,816 KB |
| 最終ジャッジ日時 | 2024-06-26 09:29:57 |
| 合計ジャッジ時間 | 26,389 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 92 |
コンパイルメッセージ
Main.rb:26: warning: mismatched indentations at 'end' with 'while' at 24 Main.rb:29: warning: mismatched indentations at 'end' with 'def' at 22 Main.rb:33: warning: mismatched indentations at 'end' with 'while' at 31 Main.rb:52: warning: `-' after local variable or literal is interpreted as binary operator Main.rb:52: warning: even though it seems like unary operator Syntax OK
ソースコード
#!/usr/bin/ruby
def is_minimam(a1,b1,a2,b2,a3,b3)
(a2-a1)*(b3-b2)>=(b2-b1)*(a3-a2)
end
def is_maximam(a1,b1,a2,b2,a3,b3)
is_minimam(-a1,-b1,-a2,-b2,-a3,-b3)
end
def check(l1,l2,l3)
is_minimam(l1[0],l1[1],l2[0],l2[1],l3[0],l3[1])
end
def calc(l,now)
l[0]*now+l[1]
end
class ConvexHullTrick
def initialize
@deque=100000.times.map{[0,0]}
@s=0
@t=0
end
def add(a,b)
l=[a,b]
while @s+1<@t&&check(@deque[@t-2],@deque[@t-1],l)
@t-=1
end
@deque[@t]=l
@t+=1
end
def get(now)
while @s+1<@t&&calc(@deque[@s],now)>=calc(@deque[@s+1],now)
@s+=1
end
calc(@deque[@s],now)
end
end
n,a,b,w=gets.split.map(&:to_i)
d=gets.split.map(&:to_i)
deq=ConvexHullTrick.new
dp=[Float::INFINITY]*(n+1)
dp[0]=w
deq.add( - 0 * b,dp[0] + 0 * a + b * 0 * (0 - 1) / 2)
n.times{|i|
dp[i + 1] = d[i] - i * a + ( b * i * (i + 1)) / 2 + deq.get(i)
deq.add( - (i + 1) * b , dp[i + 1] + (i + 1) * a + ( b * (i + 1) * i) / 2)
}
res=Float::INFINITY
(n+1).times{|i|
res=[res,dp[i] - a * (n -i) + b * (n - i) * (n - i + 1) / 2].min
}
p res