結果

問題 No.281 門松と魔法(1)
ユーザー yuppe19 😺
提出日時 2016-07-17 06:11:46
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,432 bytes
コンパイル時間 652 ms
コンパイル使用メモリ 59,468 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-06 20:46:53
合計ジャッジ時間 2,016 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます
コンパイルメッセージ
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