結果

問題 No.281 門松と魔法(1)
ユーザー te-shte-sh
提出日時 2017-06-08 10:28:59
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,098 bytes
コンパイル時間 645 ms
コンパイル使用メモリ 86,160 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 14:01:04
合計ジャッジ時間 2,756 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 1 ms
4,380 KB
testcase_47 AC 2 ms
4,384 KB
testcase_48 AC 1 ms
4,380 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 1 ms
4,376 KB
testcase_53 AC 2 ms
4,376 KB
testcase_54 AC 1 ms
4,376 KB
testcase_55 AC 1 ms
4,376 KB
testcase_56 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto d = readln.chomp.to!int;
  auto hi = 3.iota.map!(_ => readln.chomp.to!int).array;

  if (d == 0) {
    if (hi[0] != hi[2] &&
        (hi[0] > hi[1] && hi[2] > hi[1] ||
         hi[0] < hi[1] && hi[2] < hi[1]))
      writeln(0);
    else
      writeln(-1);

    return;
  }

  auto r = min(calc1(d, hi.dup), calc2(d, hi.dup));
  writeln(r == int.max ? -1 : r);
}

auto calc1(int d, int[] hi)
{
  auto r = 0;

  if (hi[0] == hi[2]) {
    ++r;
    hi[0] = max(hi[0] - d, 0);
  }

  auto mh = min(hi[0], hi[2]);
  if (mh == 0) return int.max;
  if (hi[1] < mh) return r;

  auto s = (hi[1] - mh + d) / d;
  hi[1] = max(hi[1] - d * s, 0);
  r += s;

  return r;
}

auto calc2(int d, int[] hi)
{
  auto r = 0;

  if (hi[1] == 0) return int.max;

  foreach (i; [0, 2]) {
    if (hi[i] < hi[1]) continue;

    auto s = (hi[i] - hi[1] + d) / d;
    hi[i] = max(hi[i] - d * s, 0);
    r += s;
  }

  if (hi[0] == hi[2]) {
    ++r;
    hi[0] = max(hi[0] - d, 0);
  }

  if (hi[0] == hi[2]) return int.max;

  return r;
}
0