結果

問題 No.297 カードの数式
ユーザー motimoti
提出日時 2015-11-16 17:32:32
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,101 bytes
コンパイル時間 903 ms
コンパイル使用メモリ 106,140 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-11 16:47:35
合計ジャッジ時間 2,316 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define minimize(a, x) a = std::min(a, x)
#define maximize(a, x) a = std::max(a, x)

using ll  = long long;
ll const inf = 1LL<<59;

int main() {

  int N; cin >> N;
  vector<char> v(N); rep(i, N) cin >> v[i];
  sort(all(v));

  vector<ll> dp1min(1<<N, inf), dp1max(1<<N, -inf);
  vector<ll> dp2min(1<<N, inf), dp2max(1<<N, -inf);

  REP(S, 1, 1 << N) {
    rep(i, N) {
      if((S >> i & 1) && v[i] == '+') {
        const int rS = S - (1 << i);
        for(int j=rS; j>0; (j-=1) &= rS) {
          minimize(dp1min[S], dp1min[j] + dp1min[rS - j]);
          maximize(dp1max[S], dp1max[j] + dp1max[rS - j]);
        }
      }
    }
    rep(i, N) {
      if((S >> i & 1) && v[i] == '-') {
        const int rS = S - (1 << i);
        for(int j=rS; j>0; (j-=1) &= rS) {
          minimize(dp1min[S], dp1min[j] - dp1max[rS - j]);
          maximize(dp1max[S], dp1max[j] - dp1min[rS - j]);
        }
      }
    }

    ll n1 = 0; int bs = 0;
    rep(i, N) {
      if((S >> i & 1) && isdigit(v[i])) {
        bs |= 1 << i;
        n1 = n1 * 10 + (v[i] - '0');
      }
    }

    ll n2 = 0;
    for(int i=N-1; i>=0; i--) {
      if(S >> i & 1 && isdigit(v[i])) {
        bs |= 1 << i;
        n2 = n2 * 10 + (v[i] - '0');
      }
    }

    if(S == bs) {
      dp1min[S] = min({dp1min[S], n1, n2});
      dp1max[S] = max({dp1max[S], n1, n2});
      dp2min[S] = min({dp2min[S], n1, n2});
      dp2max[S] = max({dp2max[S], n1, n2});
    }
    
  }

  cout << dp1max[(1 << N) - 1] << " " << dp1min[(1 << N) - 1] << endl;

  return 0;
}
0