結果

問題 No.966 引き算をして門松列(その1)
ユーザー ngtkana
提出日時 2020-01-13 20:49:01
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 2,501 bytes
コンパイル時間 2,507 ms
コンパイル使用メモリ 193,268 KB
最終ジャッジ日時 2025-01-08 17:36:17
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

#define DEBUG 1
#include <bits/stdc++.h>
#define loop(n) for (lint ngtkana_is_a_genius = 0; ngtkana_is_a_genius < lint(n); ngtkana_is_a_genius++)
#define rep(i, begin, end) for (lint i = lint(begin); (i) < lint(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto&& a, auto b){ if (a > b) {a = b; return true;} return false; };
auto cmx = [](auto&& a, auto b){ if (a < b) {a = b; return true;} return false; };
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) { std::cerr << " " << head; debug_impl(tail...); }
#if DEBUG
#define debug(...)\
  do {\
    std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
    debug_impl(__VA_ARGS__);\
    std::cerr << std::noboolalpha;\
  } while (false)
#else
#define debug(...) {}
#endif

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  int inf = std::numeric_limits<int>::max();

  int q; std::cin >> q;
  loop(q) {
    int a, b, c;
    std::cin >> a >> b >> c;
    int ans = -2;
    if (a > c) std::swap(a, c);
    if (a==c) {
      if (a==b) {
        ans = 3 <= a ? 3 : -1;
      } else if (b==a-1) {
        ans = 2 <= b ? 2 : -1;
      } else {
        ans = 2 <= a ? 1 : -1;
      }
    } else {
      if (b < a || c < b) {
        ans = 0;
      } else {
        int now =  inf;
        if (2 <= a) cmn(now, b - a + 1);
        if (2 <= b - a) cmn(now, c - b + 1);
        if (now==inf) {
          ans = -1;
        } else {
          ans = now;
        }
      }
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

/*
a <= c としてよいです。
a = c の場合:
  3 つとも同じ場合は、値が 3 以上なら答えは 3、2以下なら不可能です。
  b = a - 1 の場合は、 b が 2 以上なら a, b をそれぞれ 1 ずつ下げるので 2、1 なら不可能です。
  その他の場合は、a だけ下げれば良いので、2<=a ? 1 : -1 です。
a < c の場合:
  b < a or a < c の場合は、もともと門松なので、0 です。
  それ以外の場合は、b か c の好きな方を下げます。
  2 <= a のとき、b を a - 1 まで下げられるので、b - a + 1 が候補です。
  2 <= b - a のとき、c を b - 1 まで下げられるので、c - b + 1 が候補です。
  どちらもできないときは、詰んでいます。
*/
0