結果

問題 No.297 カードの数式
ユーザー hanorverhanorver
提出日時 2015-11-06 23:31:30
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,590 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 67,780 KB
実行使用メモリ 4,480 KB
最終ジャッジ日時 2023-10-11 14:58:07
合計ジャッジ時間 1,710 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

int max(std::list<int> v, int plus, int minus){
    v.sort();

    int ans = 0;
    while(v.size() != plus + minus){
        ans = ans * 10 + v.back();
        v.pop_back();
    }

    while(plus){
        ans += v.back();
        v.pop_back();
        plus--;
    }

    v.push_back(ans);

    for (size_t i = 0; i < minus; i++) {
        int num = v.back(); v.pop_back();
        int num2 = v.back(); v.pop_back();
        v.push_back(num - num2);
        v.sort();
    }

    return v.front();
}

int min(std::list<int> v, int plus, int minus){
    v.sort();

    for (size_t i = 0; i < plus; i++) {
        int num = v.front(); v.pop_front();
        int num2 = v.front(); v.pop_front();
        v.push_back(num2 + num);
        v.sort();
    }

    int ans = 0;
    while(v.size() != minus){
        ans = ans * 10 + v.back();
        v.pop_back();
    }

    v.push_back(ans);
    v.sort();

    while(v.size() != 1){
        int mi = v.front();v.pop_front();
        int ma = v.front();v.pop_front();
        v.push_back(mi - ma);
        v.sort();
    }

    return v.front();
}

int main(){
    int n;
    std::cin >> n;

    std::list<int> v;
    int plus = 0, minus = 0;
    for(int i = 0; i < n; i++) {
        char c;
        std::cin >> c;
        if(c == '+') plus++;
        else if(c == '-') minus++;
        else v.push_back(c - '0');
    }

    std::list<int> v2 = v;
    int m = max(v, plus, minus);
    int mi = min(v2, plus, minus);

    std::cout << m << " " << mi << std::endl;
    return 0;
}
0