結果

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

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    for (int lo = 0; lo < t; ++lo) {
        vector<ll> a(3);
        for (int i = 0; i < 3; ++i) {
            cin >> a[i];
        }

        if (a[0] == a[1] && a[1] == a[2]) {
            if (a[1] <= 2) {
                cout << -1 << "\n";
            } else {
                cout << 3 << "\n";
            }
            continue;
        }

        if (a[2] < a[0]) swap(a[0], a[2]);

        ll cnt = 0;
        if (a[0] == a[2]) {
            a[0]--;
            cnt++;
        }
        if (a[0] == 0) {
            cout << -1 << "\n";
            continue;
        }

        if (a[1] == a[0]) {
            if (a[1] <= 1) cout << -1 << "\n";
            else cout << cnt + 1 << "\n";
            continue;
        }

        if (a[2] == a[1]) {
            if (a[2] <= 1) {
                cout << -1 << "\n";
                continue;
            } else {
                a[2]--;
                cnt++;
            }
        }

        if (a[0] == a[2]) {
            a[0]--;
            cnt++;
        }
        if (a[0] == 0) {
            cout << -1 << "\n";
            continue;
        }

        if (a[2] < a[1] || a[0] > a[1]) {
            cout << cnt << "\n";
        } else {
            ll ans = 1e18;
            if (a[0] >= 2) ans = min(ans, cnt + a[1] - a[0] + 1);
            if (a[1] >= 2) {
                ll tmp = cnt + a[2] - a[1] + 1;
                if (a[0] == a[1] - 1) {
                    if (a[0] == 1) tmp = 1e18;
                    else tmp++;
                }
                ans = min(ans, tmp);
            }
            cout << (ans >= 1e18 ? -1 : ans) << "\n";
        }
    }

    return 0;
}
0