結果

問題 No.281 門松と魔法(1)
ユーザー k
提出日時 2021-03-15 22:49:00
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,496 bytes
コンパイル時間 1,797 ms
コンパイル使用メモリ 197,436 KB
最終ジャッジ日時 2025-01-19 17:26:14
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const long long INF = 1LL<<50;

long long minimize(int d, vector<int> h) {
  long long ret = 0;

  if (h[0] == h[2]) {
    h[0] = max(h[0] - d, 0);
    ++ret;
  }
  
  if (h[1] < min(h[0], h[2])) {
    return ret;
  }

  int t = (h[1] - min(h[0], h[2])) / d + 1;
  ret += t;
  h[1] = max(h[1] - t * d, 0);

  if (h[0] != h[1] && h[1] != h[2] && h[2] != h[0] && h[1] < min(h[0], h[2]))
    return ret;
  
  return INF;
}

long long maximize(int d, vector<int> h) {
  long long ret = 0;

  if (h[0] >= h[1]) {
    int t = (h[0] - h[1]) / d + 1;
    ret += t;
    h[0] = max(0, h[0] - t * d);
  }

  if (h[2] >= h[1]) {
    int t = (h[2] - h[1]) / d + 1;
    ret += t;
    h[2] = max(0, h[2] - t * d);
  }

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

  if (h[0] != h[1] && h[1] != h[2] && h[2] != h[0] && h[1] > max(h[0], h[2]))
    return ret;
  
  return INF;
}

long long zero(int d, vector<int> h) {
  if (h[0] == h[1] || h[1] == h[2] || h[2] == h[0])
    return -1;
  if (h[1] < min(h[0], h[2]) || h[1] > max(h[0], h[2]))
    return 0;
  return -1;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  
  int d;
  vector<int> h(3);

  cin >> d;
  for (int i = 0; i < 3; i++)
    cin >> h[i];

  if (d == 0) {
    cout << zero(d, h) << endl;
  } else {
    long long ret = min(minimize(d, h), maximize(d, h));
    if (ret == INF)
      cout << -1 << endl;
    else
      cout << ret << endl;
  }  

  return 0;
}
0