結果

問題 No.966 引き算をして門松列(その1)
ユーザー batsumarubatsumaru
提出日時 2020-01-13 21:10:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 2,054 bytes
コンパイル時間 1,823 ms
コンパイル使用メモリ 168,836 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 13:45:10
合計ジャッジ時間 2,578 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 16 ms
4,380 KB
testcase_05 AC 21 ms
4,380 KB
testcase_06 AC 27 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto&(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

void solve() {
  vector<ll> a(3);
  REP(i, 3) { cin >> a[i]; }
  if (a[0] == a[1] && a[1] == a[2]) {
    // 1 1 1 -> -1
    // 2 2 2 -> -1
    if (a[0] <= 2) {
      cout << -1 << endl;
      return;
    }
    // 3 3 3 -> 3
    cout << 3 << endl;
    return;
  }

  // 両側2つ等しい
  // 2 1 2 -> -1
  // 1 2 1 -> -1
  if (a[0] == 1 && a[1] >= 2 && a[2] == 1 ||
      a[0] == 2 && a[1] == 1 && a[2] == 2) {
    cout << -1 << endl;
    return;
  }
  if (a[0] == a[2]) {
    if (a[0] == a[1] + 1) {
      // 4 3 4 -> 2
      cout << 2 << endl;
      return;
    }
    // 6 3 6 -> 1
    // 3 4 3 -> 1
    cout << 1 << endl;
    return;
  }

  ll mini = 1e18, maxi = -1;
  REP(i, 3) {
    mini = min(mini, a[i]);
    maxi = max(maxi, a[i]);
  }

  // 1 1 6 -> -1
  if (a[0] == a[1] && a[0] == 1 || a[1] == a[2] && a[2] == 1) {
    cout << -1 << endl;
    return;
  }

  // 2 2 1 -> -1
  // 1 2 2 -> -1
  if (a[1] == 2 && (a[0] == 2 && a[2] == 1 || a[0] == 1 && a[2] == 2)) {
    cout << -1 << endl;
    return;
  }

  // 3 3 2 -> 2
  if (a[0] == a[1] && a[1] - 1 == a[2] || a[1] == a[2] && a[1] - 1 == a[0]) {
    cout << 2 << endl;
    return;
  }

  if (a[0] == a[1] || a[1] == a[2]) {
    cout << 1 << endl;
    return;
  }

  // 全部異なる
  // 1 4 3
  // 4 2 3
  if (a[1] > a[0] && a[1] > a[2] || a[1] < a[0] && a[1] < a[2]) {
    cout << 0 << endl;
    return;
  }

  if (mini == 1) {
    if (a[1] == 2) {
      cout << -1 << endl;
      return;
    }
    cout << maxi - a[1] + 1 << endl;
    return;
  }
  cout << min(a[1] - mini + 1, maxi - a[1] + 1) << endl;
}

int main() {
  ll n;
  cin >> n;
  while (n--) solve();
}
0