結果

問題 No.297 カードの数式
ユーザー hanorverhanorver
提出日時 2015-11-08 14:49:03
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,163 bytes
コンパイル時間 632 ms
コンパイル使用メモリ 70,808 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-11 15:20:08
合計ジャッジ時間 1,824 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

long long max(std::list<long long> l, long long plus, long long minus){
    l.sort();

    // '+'と'-'の数+1個の項を作る
    if(l.size() != minus + plus + 1){
        long long num = 0;
        while(l.size() != minus + plus){
            num = num * 10 + l.back();
            l.pop_back();
        }
        l.push_front(num);
    }

    l.sort();
    l.reverse();
    while(minus){
        long long num = l.front(); l.pop_front();
        num -= l.front(); l.pop_front();
        l.push_front(num);
        minus--;
    }

    long long ans = l.front(); l.pop_front();
    while(!l.empty()) {
        ans += l.front(); l.pop_front();
    }

    return ans;
}

long long min(std::list<long long> l, long long plus, long long minus){
    l.sort();
    if(minus == 0){
        std::vector<long long> v(plus + 1); // 計算には'+'+1個の項が必要
        long long n = 0;
        while(!l.empty()){
            v[n] = v[n] * 10 + l.front();
            l.pop_front();
            n++;
            n %= plus + 1;
        }
        while(v.size() != 1){
            long long num = v.back(); v.pop_back();
            num += v.back(); v.pop_back();
            v.push_back(num);
        }
        return v.front();
    }

    if(l.size() != plus + minus + 1){
        long long num = 0;
        while(l.size() != plus + minus){
            num = num * 10 + l.back();
            l.pop_back();
        }
        l.insert(++l.begin(), num);
    }

    int ans = l.front(); l.pop_front();
    while(minus){
        ans -= l.front(); l.pop_front();
        minus--;
    }

    for(long long i = 0; i < plus; i++){
        ans += l.front();
        l.pop_front();
    }

    return ans;
}

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

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

    std::cout << max(l, plus, minus) << " " << min(l, plus, minus) << std::endl;
    return 0;
}
0