結果

問題 No.968 引き算をして門松列(その3)
ユーザー pekempeypekempey
提出日時 2020-01-13 20:40:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 468 ms / 2,000 ms
コード長 1,218 bytes
コンパイル時間 1,678 ms
コンパイル使用メモリ 169,464 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-23 13:11:18
合計ジャッジ時間 5,056 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,384 KB
testcase_03 AC 202 ms
4,380 KB
testcase_04 AC 216 ms
4,380 KB
testcase_05 AC 219 ms
4,380 KB
testcase_06 AC 219 ms
4,376 KB
testcase_07 AC 220 ms
4,376 KB
testcase_08 AC 347 ms
4,376 KB
testcase_09 AC 421 ms
4,376 KB
testcase_10 AC 408 ms
4,380 KB
testcase_11 AC 468 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define range(a) a.begin(), a.end()

bool is_kadomatu(ll x, ll y, ll z) {
  return x != y and x != z and y != z and ((x < y and y > z) or (x > y and y < z));
}

ll solve(ll a, ll b, ll c, ll p, ll q, ll r) {
  vector<ll> S;
  rep(i, 3) {
    for (int j = -2; j <= 2; j++) {
      if (a + j >= 1) S.push_back(a + j);
      if (b + j >= 1) S.push_back(b + j);
      if (c + j >= 1) S.push_back(c + j);
    }
  }
  ll res = LLONG_MAX;
  for (ll x : S) if (a <= x) {
    for (ll y : S) if (b <= y) {
      for (ll z : S) if (c <= z) {
        ll s = x - a;
        ll t = y - b;
        ll u = z - c;
        if (a - t - u <= 0) continue;
        if (b - s - u <= 0) continue;
        if (c - s - t <= 0) continue;
        if (is_kadomatu(x, y, z)) {
          res = min(res, s*q + t*r + u*p);
        }
      }
    }
  }
  return res;
}

int main() {
  int T; cin >> T;
  while (T--) {
    ll a, b, c, p, q, r; cin >> a >> b >> c >> p >> q >> r;
    ll ans = solve(a, b, c, p, q, r);
    if (ans == LLONG_MAX) ans = -1;
    cout << ans << '\n';
  }
}
0