結果

問題 No.966 引き算をして門松列(その1)
ユーザー simansiman
提出日時 2020-12-18 15:56:12
言語 Ruby
(3.3.0)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 826 bytes
コンパイル時間 57 ms
コンパイル使用メモリ 11,976 KB
実行使用メモリ 16,196 KB
最終ジャッジ日時 2023-10-21 08:09:43
合計ジャッジ時間 1,612 ms
ジャッジサーバーID
(参考情報)
judge9 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
16,084 KB
testcase_01 AC 80 ms
16,084 KB
testcase_02 AC 72 ms
16,084 KB
testcase_03 AC 72 ms
16,084 KB
testcase_04 AC 88 ms
16,192 KB
testcase_05 AC 94 ms
16,192 KB
testcase_06 AC 104 ms
16,196 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:68: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

T = gets.to_i

# /\ cost
def f(a, b, c)
  cost = 0

  if a >= b
    d = a - b + 1
    a -= d
    cost += d
  end

  if c >= b
    d = c - b + 1
    c -= d
    cost += d
  end

  if a == c
    cost += 1
    a -= 1
  end

  return Float::INFINITY if a <= 0 || c <= 0

  cost
end

# \/ cost
def g(a, b, c)
  cost = 0

  if a == c
    a -= 1
    cost += 1
  end

  return Float::INFINITY if a <= 0

  if a <= b
    d = b - a + 1
    b -= d
    cost += d
  end

  if c <= b
    d = b - c + 1
    b -= d
    cost += d
  end

  return Float::INFINITY if b <= 0

  cost
end

T.times do
  a, b, c = gets.split.map(&:to_i)

  if a > b && c > b && a != c
    puts 0
  elsif a < b && c < b && a != c
    puts 0
  else
    v = [f(a, b, c), g(a, b, c)].min

    if v == Float::INFINITY
      puts -1
    else
      puts v
    end
  end
end
0