結果

問題 No.966 引き算をして門松列(その1)
ユーザー tonegawatonegawa
提出日時 2020-08-18 14:39:28
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 1,445 bytes
コンパイル時間 668 ms
コンパイル使用メモリ 70,060 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-20 07:33:59
合計ジャッジ時間 1,158 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>

using lint = long long;
constexpr lint INF = 1LL << 40;

void solve() {
    int q;
    std::cin >> q;
    while (q--) {
        lint a, b, c;
        std::cin >> a >> b >> c;

        lint ans = INF;
        {
            // b is max
            lint na = a, nb = b, nc = c;
            lint cost = 0;

            if (na >= nb) {
                cost += na - nb + 1;
                na = nb - 1;
            }
            if (nc >= nb) {
                cost += nc - nb + 1;
                nc = nb - 1;
            }

            if (na == nc) {
                ++cost;
                --na;
            }

            if (na > 0 && nb > 0 && nc > 0) {
                ans = std::min(ans, cost);
            }
        }

        {
            // b is min
            lint na = a, nb = b, nc = c;
            lint cost = 0;

            if (na == nc) {
                ++cost;
                --na;
            }

            if (na <= nb) {
                cost += nb - na + 1;
                nb = na - 1;
            }

            if (nc <= nb) {
                cost += nb - nc + 1;
                nb = nc - 1;
            }

            if (na > 0 && nb > 0 && nc > 0) {
                ans = std::min(ans, cost);
            }
        }

        std::cout << (ans == INF ? -1 : ans) << std::endl;
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0