結果

問題 No.2024 Xer
ユーザー SSRS
提出日時 2022-07-29 22:26:59
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,795 bytes
コンパイル時間 1,599 ms
コンパイル使用メモリ 174,692 KB
実行使用メモリ 27,984 KB
最終ジャッジ日時 2024-07-19 15:34:23
合計ジャッジ時間 8,132 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 37 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
vector<int> dfs(vector<int> A, int X, int p){
  if (A.empty()){
    return A;
  }
  if (p == -1){
    if (A.size() == 1){
      return A;
    } else {
      return vector<int>(0);
    }
  }
  vector<int> B0, B1;
  for (int x : A){
    if ((x >> p & 1) == 0){
      B0.push_back(x);
    } else {
      B1.push_back(x);
    }
  }
  if ((X >> p & 1) == 0){
    vector<int> ans0 = dfs(B0, X, p - 1);
    vector<int> ans1 = dfs(B1, X, p - 1);
    if (!B0.empty() && ans0.empty()){
      return vector<int>(0);
    }
    if (!B1.empty() && ans1.empty()){
      return vector<int>(0);
    }
    for (int x : ans1){
      ans0.push_back(x);
    }
    int cnt = ans0.size();
    bool ok = true;
    int X2 = X & ((1 << (p + 1)) - 1);
    for (int i = 0; i < cnt - 1; i++){
      if (ans0[i] >= (ans0[i + 1] ^ X2)){
        ok = false;
      }
      if ((ans0[i] ^ X2) >= ans0[i + 1]){
        ok = false;
      }
    }
    if (!ok){
      return vector<int>(0);
    }
    return ans0;
  } else {
    int cnt0 = B0.size();
    int cnt1 = B1.size();
    if (abs(cnt0 - cnt1) >= 2){
      return vector<int>(0);
    }
    vector<int> C0 = dfs(B0, X, p - 1);
    vector<int> C1 = dfs(B1, X, p - 1);
    if (!B0.empty() && C0.empty()){
      return vector<int>(0);
    }
    if (!B1.empty() && C1.empty()){
      return vector<int>(0);
    }
    vector<int> ans1, ans2;
    if (cnt0 > cnt1){
      for (int i = 0; i < cnt1; i++){
        ans1.push_back(C0[i]);
        ans1.push_back(C1[i]);
      }
      ans1.push_back(C0[cnt1]);
    } else if (cnt1 > cnt0){
      for (int i = 0; i < cnt0; i++){
        ans1.push_back(C1[i]);
        ans1.push_back(C0[i]);
      }
      ans1.push_back(C1[cnt0]);
    } else {
      for (int i = 0; i < cnt0; i++){
        ans1.push_back(C0[i]);
        ans1.push_back(C1[i]);
        ans2.push_back(C1[i]);
        ans2.push_back(C0[i]);
      }
    }
    int cnt = cnt0 + cnt1;
    bool ok1 = true, ok2 = true;
    int X2 = X & ((1 << (p + 1)) - 1);
    for (int i = 0; i < cnt - 1; i++){
      if (ans1[i] >= (ans1[i + 1] ^ X2)){
        ok1 = false;
      }
      if ((ans1[i] ^ X2) >= ans1[i + 1]){
        ok1 = false;
      }
      if (!ans2.empty()){
        if (ans2[i] >= (ans2[i + 1] ^ X2)){
          ok2 = false;
        }
        if ((ans2[i] ^ X2) >= ans2[i + 1]){
          ok2 = false;
        }
      }
    }
    if (!ans1.empty() && ok1){
      return ans1;
    } else if (!ans2.empty() && ok2){
      return ans2;
    } else {
      return vector<int>(0);
    }
  }
}
int main(){
  int N, X;
  cin >> N >> X;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  vector<int> res = dfs(A, X, 29);
  if (!res.empty()){
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
}
0