結果

問題 No.966 引き算をして門松列(その1)
ユーザー MisterMister
提出日時 2020-04-21 23:30:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,445 bytes
コンパイル時間 553 ms
コンパイル使用メモリ 70,088 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-17 20:20:18
合計ジャッジ時間 1,186 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 14 ms
5,376 KB
testcase_05 AC 15 ms
5,376 KB
testcase_06 AC 19 ms
5,376 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