結果

問題 No.968 引き算をして門松列(その3)
ユーザー drken1215drken1215
提出日時 2020-01-13 21:41:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 1,361 bytes
コンパイル時間 962 ms
コンパイル使用メモリ 81,852 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 15:49:27
合計ジャッジ時間 2,108 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 23 ms
4,380 KB
testcase_04 AC 25 ms
4,380 KB
testcase_05 AC 25 ms
4,380 KB
testcase_06 AC 25 ms
4,376 KB
testcase_07 AC 28 ms
4,380 KB
testcase_08 AC 46 ms
4,380 KB
testcase_09 AC 56 ms
4,380 KB
testcase_10 AC 57 ms
4,380 KB
testcase_11 AC 62 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const long long INF = 1LL<<60;

// (a, b, c) が門松列かどうか
bool isok(long long a, long long b, long long c) {
    if (a < 1 || b < 1 || c < 1) return false;
    if (a == b) return false;
    if (b == c) return false;
    if (c == a) return false;
    if (a < b && b < c) return false;
    if (a > b && b > c) return false;
    return true;
}

long long A, B, C, X, Y, Z;

long long solve() {
    long long res = INF;
    vector<long long> alt;
    for (int it = 0; it <= 2; ++it) {
        alt.push_back(A + it);
        alt.push_back(B + it);
        alt.push_back(C + it);
    }
    
    for (auto a : alt) {
        for (auto b : alt) {
            for (auto c : alt) {
                if (A > a) continue;
                if (B > b) continue;
                if (C > c) continue;     
                long long sum = (a-A) + (b-B) + (c-C); // 操作回数
                if (isok(a-sum, b-sum, c-sum)) {
                    res = min(res, (a-A) * Y + (b-B) * Z + (c-C) * X);
                }
            }
        }
    }
    if (res < INF) return res;
    else return -1;
}

int main() {
    int T; cin >> T;
    for (int _ = 0; _ < T; ++_) {
        cin >> A >> B >> C >> X >> Y >> Z;
        cout << solve() << endl;
    }
}
0