結果

問題 No.966 引き算をして門松列(その1)
ユーザー mmn15277198mmn15277198
提出日時 2021-04-11 15:49:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,306 bytes
コンパイル時間 676 ms
コンパイル使用メモリ 84,716 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-09 18:06:06
合計ジャッジ時間 1,692 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <map>
#include <cstdio>
#include <cmath>
#include <iomanip>

using namespace std;

const int INF = 1001001001;

int A , B , C;

int check (int a , int b , int c) {
    return (a != b && b != c && c != a && (b > a && b > c || b < a && b < c) && a > 0 && b > 0 && c > 0);
}

int solve2 (int a , int b , int c) {
    int ret = 0;
    if (a == c) c -= 1;
    if (a >= b) a -= (a - b + 1);
    if (c >= b) c -= (c - b + 1);
    if (check(a , b , c) == true) {
        return abs(A - a) + abs(B - b) + abs(C - c);
    } else {
        return INF;
    }
}

int solve3 (int a , int b , int c) {
    if (b >= a) b -= (b - a + 1);
    if (b >= c) b -= (b - c + 1);
    if (a == c) a -= 1;
    if (b >= a) b -= (b - a + 1);
    if (b >= c) b -= (b - c + 1);
    if (check(a , b , c) == true) {
        return abs(A - a) + abs(B - b) + abs(C - c);
    } else {
        return INF;
    }
}

void solve() {
   
    cin >> A >> B >> C;
    int cost1 = solve2 (A , B , C);
    int cost2 = solve3 (A , B , C);
    int cost = min(cost1 , cost2);
    if (cost == INF) cost = -1;
    cout << cost << endl;
    return;
}

int main() {
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        solve();
    }
    return 0;
}




0