結果

問題 No.297 カードの数式
ユーザー MisterMister
提出日時 2020-08-01 23:43:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,276 bytes
コンパイル時間 959 ms
コンパイル使用メモリ 83,748 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-22 20:39:13
合計ジャッジ時間 3,087 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 2 ms
4,380 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

using lint = long long;

void solve() {
    int n;
    std::cin >> n;

    std::vector<int> ds;
    int pnum = 0, nnum = 0;

    while (n--) {
        char c;
        std::cin >> c;

        switch (c) {
            case '+':
                ++pnum;
                break;
            case '-':
                ++nnum;
                break;
            default:
                ds.push_back(c - '0');
        }
    }

    std::sort(ds.rbegin(), ds.rend());

    std::vector<lint> xs;
    for (int i = 0; i < nnum + pnum; ++i) {
        xs.push_back(ds.back());
        ds.pop_back();
    }

    lint k = 0;
    for (auto d : ds) k = k * 10 + d;
    xs.push_back(k);

    lint min = 0;
    for (int i = 0; i < (int)xs.size(); ++i) {
        if (i <= pnum) {
            min += xs[i];
        } else {
            min -= xs[i];
        }
    }

    std::reverse(xs.begin(), xs.end());
    lint max = 0;
    for (int i = 0; i < (int)xs.size(); ++i) {
        if (i <= pnum) {
            max += xs[i];
        } else {
            max -= xs[i];
        }
    }

    std::cout << max << " " << min << "\n";
}

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

    solve();

    return 0;
}
0