結果

問題 No.1246 ANDORゲーム(max)
ユーザー tnakao0123tnakao0123
提出日時 2020-10-03 14:07:43
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 570 ms / 2,000 ms
コード長 1,504 bytes
コンパイル時間 934 ms
コンパイル使用メモリ 102,368 KB
実行使用メモリ 202,580 KB
最終ジャッジ日時 2024-07-18 04:25:29
合計ジャッジ時間 8,869 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1246.cc:  No.1246 ANDORゲーム(max) - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

typedef long long ll;
typedef map<int,ll> mil;

/* global variables */

int as[MAX_N];
mil dp[MAX_N + 1];

/* subroutines */

/* main */

int main() {
  int n, t;
  scanf("%d%d", &n, &t);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  dp[0][t] = 0;
  for (int i = 0; i < n; i++) {
    mil &dp0 = dp[i], &dp1 = dp[i + 1];
    for (mil::iterator mit0 = dp0.begin(); mit0 != dp0.end(); mit0++) {
      int x0 = mit0->first;

      int ax = x0 & as[i];
      ll ad = mit0->second + abs(ax - x0);
      mil::iterator amit = dp1.find(ax);
      if (amit == dp1.end()) dp1[ax] = ad;
      else if (amit->second < ad) amit->second = ad;

      int ox = x0 | as[i];
      ll od = mit0->second + abs(ox - x0);
      mil::iterator omit = dp1.find(ox);
      if (omit == dp1.end()) dp1[ox] = od;
      else if (omit->second < od) omit->second = od;
    }
  }

  ll maxd = 0;
  for (mil::iterator mit = dp[n].begin(); mit != dp[n].end(); mit++)
    if (maxd < mit->second) maxd = mit->second;

  printf("%lld\n", maxd);
  return 0;
}
0