結果

問題 No.966 引き算をして門松列(その1)
ユーザー Mister
提出日時 2020-04-21 23:30:57
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 1,445 bytes
コンパイル時間 666 ms
コンパイル使用メモリ 66,432 KB
最終ジャッジ日時 2025-01-09 22:11:58
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 5
権限があれば一括ダウンロードができます

ソースコード

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