結果

問題 No.1906 Twinkle Town
ユーザー emthrm
提出日時 2022-04-16 01:15:26
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 2,397 bytes
コンパイル時間 2,011 ms
コンパイル使用メモリ 207,892 KB
最終ジャッジ日時 2025-01-28 18:48:58
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other TLE * 33 MLE * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

void solve() {
  int n; cin >> n;
  vector<int> a(n); REP(i, n) cin >> a[i];

  map<pair<int, vector<int>>, pair<int, int>> dp;
  auto f = [&](auto&& f, const int light, const vector<int>& a, bool taro) -> int {
    if (a.empty()) return __builtin_popcount(light);
    if (dp.count({light, a})) return dp[{light, a}].first;
    int score = (taro ? -INF : INF), choice = -1;
    const int n = a.size();
    REP(i, n) {
      if (i > 0 && a[i] == a[i - 1]) continue;
      vector<int> b = a;
      b.erase(b.begin() + i);
      if (taro) {
        if (chmax(score, f(f, light | ((1 << a[i]) - 1), b, !taro))) choice = a[i];
      } else {
        if (chmin(score, f(f, light & (~0 ^ ((1 << a[i]) - 1)), b, !taro))) choice = a[i];
      }
    }
    dp[{light, a}] = {score, choice};
    return score;
  };
  cout << f(f, 0, a, true) << '\n';

  // for (const auto [key, val] : dp) {
  //   const auto [light, a] = key;
  //   cout << bitset<10>(light) << ' ';
  //   REP(i, a.size()) cout << a[i] << ",:"[i + 1 == a.size()];
  //   cout << ' ' << val.first << ' ' << val.second << '\n';
  // }

  // for (int light = 0, taro = 1; !a.empty(); taro ^= 1) {
  //   const int choice = dp[{light, a}].second;
  //   cout << choice << " \n"[a.size() == 1];
  //   a.erase(lower_bound(ALL(a), choice));
  //   if (taro) {
  //     light |= (1 << choice) - 1;
  //   } else {
  //     light &= ~0 ^ ((1 << choice) - 1);
  //   }
  // }
}

int main() {
  int t; cin >> t;
  while (t--) solve();
  return 0;
}
0