結果

問題 No.297 カードの数式
ユーザー yukiyuki
提出日時 2015-11-07 01:01:33
言語 Ruby
(3.3.0)
結果
AC  
実行時間 76 ms / 1,000 ms
コード長 663 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 11,404 KB
実行使用メモリ 15,228 KB
最終ジャッジ日時 2023-08-26 22:49:29
合計ジャッジ時間 3,079 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
15,192 KB
testcase_01 AC 74 ms
15,100 KB
testcase_02 AC 72 ms
15,192 KB
testcase_03 AC 72 ms
15,072 KB
testcase_04 AC 72 ms
15,224 KB
testcase_05 AC 75 ms
15,224 KB
testcase_06 AC 73 ms
15,196 KB
testcase_07 AC 69 ms
15,196 KB
testcase_08 AC 71 ms
15,056 KB
testcase_09 AC 71 ms
15,056 KB
testcase_10 AC 75 ms
14,992 KB
testcase_11 AC 74 ms
15,052 KB
testcase_12 AC 75 ms
15,100 KB
testcase_13 AC 71 ms
15,128 KB
testcase_14 AC 72 ms
15,228 KB
testcase_15 AC 75 ms
15,124 KB
testcase_16 AC 74 ms
15,008 KB
testcase_17 AC 74 ms
15,068 KB
testcase_18 AC 72 ms
15,076 KB
testcase_19 AC 70 ms
15,008 KB
testcase_20 AC 73 ms
15,100 KB
testcase_21 AC 72 ms
15,068 KB
testcase_22 AC 71 ms
15,056 KB
testcase_23 AC 70 ms
15,228 KB
testcase_24 AC 69 ms
15,072 KB
testcase_25 AC 70 ms
15,080 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
C = gets.split.group_by{|x| %w(+ -).include? x}
oprs = C[true]
cn = C[false]

cn = cn.sort.reverse
oprs = oprs.sort
ansmax,ansmin = [0,0]

slot = oprs.size + 1
max = cn[0..(-slot)].join.to_i
ansmax = max
cn.last(slot-1).zip(oprs) do |c,opr|
  ansmax = opr.to_sym.to_proc.call(ansmax,c.to_i)
end

if oprs.last == "-"
  ansmin = -max
  oprs.pop
  oprs = oprs.reverse << "+"
  cn.last(slot-1).zip(oprs) do |c,opr|
    ansmin = opr.to_sym.to_proc.call(ansmin,c.to_i)
  end
else
  a = Array.new(slot){ "" }
  index = (0..slot-1).cycle
  cn.each{|c| i = index.next; a[i] = c + a[i]}
  ansmin = a.map(&:to_i).inject(:+)
end

puts [ansmax,ansmin].join(" ")
0