結果

問題 No.281 門松と魔法(1)
ユーザー tnakao0123tnakao0123
提出日時 2016-04-13 13:53:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,695 bytes
コンパイル時間 625 ms
コンパイル使用メモリ 83,612 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 12:40:59
合計ジャッジ時間 2,053 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 281.cc: No.281 門松と魔法(1) - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int INF = 1 << 30;

/* typedef */

/* global variables */

/* subroutines */

inline bool is_kadomatsu(int h0, int h1, int h2) {
  return
    (h0 != h2 && ((h0 < h1 && h1 > h2) || (h0 > h1 && h1 < h2)));
}

void finish(int ans) {
  printf("%d\n", ans);
  exit(0);
}

/* main */

int main() {
  int d, h0, h1, h2;
  cin >> d >> h0 >> h1 >> h2;

  if (d == 0) finish(is_kadomatsu(h0, h1, h2) ? 0 : -1);
  
  int cnt = 0;
  if (h0 == h2) {
    h0 = max(0, h0 - d);
    cnt++;
    if (h0 == h2) finish(-1);
  }
  if (is_kadomatsu(h0, h1, h2)) finish(cnt);

  // keep h0 < h2
  if (h0 > h2) swap(h0, h2);
  
  // h0 < h1 > h2
  int c0 = INF;
  if (h1 > 0) {
    c0 = cnt;
    int ch0 = h0, ch2 = h2;
    if (ch0 >= h1) {
      int e = (ch0 - h1) / d + 1;
      c0 += e;
      ch0 = max(ch0 - e * d, 0);
    }
    if (ch2 >= h1) {
      int e = (ch2 - h1) / d + 1;
      c0 += e;
      ch2 = max(ch2 - e * d, 0);
    }
    if (ch0 == ch2) {
      if (ch0 == 0) c0 = INF;
      else c0++;
    }
  }
  
  // h0 > h1 < h2
  int c1 = INF;
  if (h0 > 0) {
    c1 = cnt;
    int ch1 = h1;
    if (h0 <= ch1) {
      int e = (ch1 - h0) / d + 1;
      c1 += e;
      ch1 = max(ch1 - e * d, 0);
    }
  }

  int c = min(c0, c1);
  finish((c >= INF) ? -1 : c);
  return 0;
}
0