結果

問題 No.968 引き算をして門松列(その3)
ユーザー drken1215drken1215
提出日時 2020-01-13 21:15:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 4,075 bytes
コンパイル時間 1,366 ms
コンパイル使用メモリ 102,564 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 13:55:57
合計ジャッジ時間 2,284 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }

#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }

template<class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template<class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template<class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
    t = v;
}
template<class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v){
    for (auto &e : t) fill(e, v);
}


const long long INF = 1LL<<60;

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() {
    if (isok(A, B, C)) return 0;
    
    long long mi = min(min(A, B), C);
    long long ma = max(max(A, B), C);

    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);
    }
    

    if (ma - mi <= 5) {
        long long res = INF;
        for (int x = 0; x <= 10; ++x) {
            for (int y = 0; y <= 10; ++y) {
                for (int z = 0; z <= 10; ++z) {
                    if (isok(A-x-z, B-x-y, C-y-z)) {
                        chmin(res, (long long)(x*X + y*Y + z*Z));
                    }
                }
            }
        }
        if (res < INF) return res;
        else return -1;
    }
    else {
        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;
                    // A -> a, B -> b, C -> c
                    
                    long long sum = (a-A) + (b-B) + (c-C);
                    if (isok(a-sum, b-sum, c-sum)) {
                        chmin(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