結果

問題 No.297 カードの数式
ユーザー parukiparuki
提出日時 2016-07-20 22:09:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 1,000 ms
コード長 2,173 bytes
コンパイル時間 1,546 ms
コンパイル使用メモリ 169,532 KB
実行使用メモリ 19,888 KB
最終ジャッジ日時 2023-08-26 22:55:31
合計ジャッジ時間 2,809 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,484 KB
testcase_01 AC 2 ms
5,476 KB
testcase_02 AC 2 ms
5,476 KB
testcase_03 AC 2 ms
5,452 KB
testcase_04 AC 12 ms
15,696 KB
testcase_05 AC 3 ms
5,720 KB
testcase_06 AC 2 ms
5,556 KB
testcase_07 AC 2 ms
5,420 KB
testcase_08 AC 2 ms
5,412 KB
testcase_09 AC 2 ms
5,456 KB
testcase_10 AC 2 ms
5,480 KB
testcase_11 AC 2 ms
5,600 KB
testcase_12 AC 3 ms
5,660 KB
testcase_13 AC 2 ms
5,748 KB
testcase_14 AC 2 ms
5,724 KB
testcase_15 AC 3 ms
5,664 KB
testcase_16 AC 27 ms
19,804 KB
testcase_17 AC 7 ms
11,544 KB
testcase_18 AC 23 ms
19,800 KB
testcase_19 AC 2 ms
5,468 KB
testcase_20 AC 3 ms
6,036 KB
testcase_21 AC 12 ms
15,632 KB
testcase_22 AC 16 ms
19,744 KB
testcase_23 AC 2 ms
5,472 KB
testcase_24 AC 24 ms
19,788 KB
testcase_25 AC 24 ms
19,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

int N;
string input;

const ll INF = LLONG_MAX;
ll maxi[1 << 15][16][2];
ll mini[1 << 15][16][2];

void solve(){
    vll pow10(18, 1);
    rep(i, 17)pow10[i + 1] = pow10[i] * 10;

    rep(i, 1 << N)rep(j, N + 1)rep(k, 2){
        maxi[i][j][k] = -INF;
        mini[i][j][k] = INF;
    }

    maxi[0][0][0] = mini[0][0][0] = 0;

    const int fin = (1 << N) - 1;

    auto update = [&](int i, int j, int k, ll ma, ll mi){
        smax(maxi[i][j][k], ma);
        smin(mini[i][j][k], mi);
    };

    rep(mask, (1 << N) - 1)rep(curDigit, N)rep(oper, 2)if(maxi[mask][curDigit][oper] != -INF){
        ll ma = maxi[mask][curDigit][oper];
        ll mi = mini[mask][curDigit][oper];

        rep(i, N)if(!(mask >> i & 1)){
            char chara = input[i];
            int nextMask = 1 << i | mask;
            if(chara == '+' || chara == '-'){
                if(curDigit < 1 || nextMask == fin)continue;
                update(nextMask, 0, chara == '+' ? 0 : 1, ma, mi);
            } else{
                ll mul = (oper == 0 ? 1 : -1) * pow10[curDigit];
                ll nextMa = ma + mul*(chara - '0');
                ll nextMi = mi + mul*(chara - '0');
                update(nextMask, curDigit + 1, oper, nextMa, nextMi);
            }
        }
    }

    ll ansMa = -INF, ansMi = INF;
    rep(curDigit, N + 1)rep(oper, 2){
        smax(ansMa, maxi[fin][curDigit][oper]);
        smin(ansMi, mini[fin][curDigit][oper]);
    }
    cout << ansMa << ' ' << ansMi << endl;
}

int main(){
    while(cin >> N){
        input.clear();
        char chara;
        rep(i, N){
            cin >> chara;
            input += chara;
        }

        solve();
    }
}
0