結果

問題 No.281 門松と魔法(1)
ユーザー yuppe19 😺yuppe19 😺
提出日時 2016-07-17 06:11:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,432 bytes
コンパイル時間 396 ms
コンパイル使用メモリ 59,840 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-24 12:42:16
合計ジャッジ時間 1,570 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 1 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 1 ms
6,940 KB
testcase_29 AC 1 ms
6,940 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 1 ms
6,940 KB
testcase_33 AC 1 ms
6,940 KB
testcase_34 AC 1 ms
6,940 KB
testcase_35 AC 1 ms
6,940 KB
testcase_36 AC 1 ms
6,944 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 1 ms
6,940 KB
testcase_39 AC 1 ms
6,940 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 1 ms
6,944 KB
testcase_44 AC 1 ms
6,944 KB
testcase_45 AC 1 ms
6,940 KB
testcase_46 AC 1 ms
6,940 KB
testcase_47 AC 2 ms
6,940 KB
testcase_48 AC 1 ms
6,948 KB
testcase_49 AC 1 ms
6,940 KB
testcase_50 AC 1 ms
6,940 KB
testcase_51 AC 1 ms
6,940 KB
testcase_52 AC 1 ms
6,944 KB
testcase_53 AC 2 ms
6,944 KB
testcase_54 AC 1 ms
6,944 KB
testcase_55 AC 1 ms
6,944 KB
testcase_56 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:32:27: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   32 |   int d, h1, h2, h3; scanf("%d%d%d%d", &d, &h1, &h2, &h3);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;

const int inf = int(1e9) + 100;

void bye(string s) {
  puts(s.c_str());
  exit(0);
}

bool ok(int x, int y, int z) {
  bool ok1 = x > y && y < z,
       ok2 = x < y && y > z;
  return x != z && (ok1 || ok2);
}

// myをyour未満にする回数
int count(int my, int your, int d) {
  if(your == 0) { return inf; }
  if(my < your) { return 0; }
  return (my - your + d) / d;
}

// myをtimes回切る
void cut(int& my, int d, int times) {
  my = max(0, my-d*times);
}

int main(void) {
  int d, h1, h2, h3; scanf("%d%d%d%d", &d, &h1, &h2, &h3);
  assert(0 <= d  && d  <= int(1e9));
  assert(0 <= h1 && h1 <= int(1e9));
  assert(0 <= h2 && h2 <= int(1e9));
  assert(0 <= h3 && h3 <= int(1e9));
  if(ok(h1, h2, h3)) { bye("0"); }
  if(d == 0) { bye("-1"); }
  int x, y, z, p, q;

  // h2(=y)を最大にする
  int r1 = 0;
  x = h1, y = h2, z = h3;
  if(x == z) { cut(x, d, 1); ++r1; }
  p = count(x, y, d);
  r1 += p;
  cut(x, d, p);
  q = count(z, y, d);
  r1 += q;
  cut(z, d, q);
  if(!ok(x, y, z)) { r1 = inf; }

  // h2(=y)を最小にする
  int r2 = 0;
  x = h1, y = h2, z = h3;
  if(x == z) { cut(x, d, 1); ++r2; }
  p = count(y, x, d);
  r2 += p;
  cut(y, d, p);
  q = count(y, z, d);
  r2 += q;
  cut(y, d, q);
  if(!ok(x, y, z)) { r2 = inf; }

  int res = min(r1, r2);
  if(res >= inf) { res = -1; }
  printf("%d\n", res);

  return 0;
}
0