結果

問題 No.505 カードの数式2
ユーザー ukuku09
提出日時 2017-04-21 23:23:40
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,055 bytes
コンパイル時間 1,368 ms
コンパイル使用メモリ 162,356 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-14 07:19:24
合計ジャッジ時間 2,329 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

int ans = 0;
int dp[2][20];

void dfs(int idx, vint& a) {
  if(dp[0][idx] != -inf && dp[1][idx] != inf) return;
  if(idx == a.size()-1) {
    dp[0][idx] = dp[1][idx] = a[idx];
    return;
  }

  rep(i, 4) {
    if(i == 0) {
      dfs(idx+1, a);
      chmax(dp[0][idx], dp[0][idx+1] + a[idx]);
      chmax(dp[0][idx], dp[1][idx+1] + a[idx]);
      chmin(dp[1][idx], dp[0][idx+1] + a[idx]);
      chmin(dp[1][idx], dp[1][idx+1] + a[idx]);
    } else if(i == 1) {
      dfs(idx+1, a);
      chmax(dp[0][idx], dp[0][idx+1] - a[idx]);
      chmax(dp[0][idx], dp[1][idx+1] - a[idx]);
      chmin(dp[1][idx], dp[0][idx+1] - a[idx]);
      chmin(dp[1][idx], dp[1][idx+1] - a[idx]);
    } else if(i == 2) {
      dfs(idx+1, a);
      chmax(dp[0][idx], dp[0][idx+1] * a[idx]);
      chmax(dp[0][idx], dp[1][idx+1] * a[idx]);
      chmin(dp[1][idx], dp[0][idx+1] * a[idx]);
      chmin(dp[1][idx], dp[1][idx+1] * a[idx]);
    } else if(i == 3) {
      if(a[idx] == 0) continue;
      dfs(idx+1, a);
      chmax(dp[0][idx], dp[0][idx+1] / a[idx]);
      chmax(dp[0][idx], dp[1][idx+1] / a[idx]);
      chmin(dp[1][idx], dp[0][idx+1] / a[idx]);
      chmin(dp[1][idx], dp[1][idx+1] / a[idx]);
    }
  }
  return;
}

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N;
  cin >> N;
  vint a(N);
  rep(i, N) cin >> a[i];
  reverse(all(a));
  //memset(dp, -1, sizeof(dp));
  rep(i, 20) {
    dp[0][i] = -inf;
    dp[1][i] = inf;
  }
  dfs(0, a);
  cout << dp[0][0] << endl;
  //cout << dfs(0, a) << endl;

  return 0;
}
0