結果

問題 No.566 だいたい完全二分木
ユーザー 0w10w1
提出日時 2017-09-09 16:37:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,013 bytes
コンパイル時間 2,713 ms
コンパイル使用メモリ 200,148 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 00:18:41
合計ジャッジ時間 2,793 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

void insert(int v, int u, vector<vector<int>> &ch) {
  if (v < u) {
    if (ch[u][0] == -1) return ch[u][0] = v, void();
    return insert(v, ch[u][0], ch);
  } else {
    if (ch[u][1] == -1) return ch[u][1] = v, void();
    return insert(v, ch[u][1], ch);
  }
}

int max_dpt(int u, const vector<vector<int>> &ch) {
  if (u == -1) return -1;
  return max(max_dpt(ch[u][0], ch), max_dpt(ch[u][1], ch)) + 1;
}

int main() {
  ios::sync_with_stdio(false);
  int K;
  cin >> K;
  vector<int> ans;
  for (int i = 0; i + 1 < 1 << K; ++i) {
    ans.emplace_back(i);
  }
  while (true) {
    random_shuffle(ans.begin(), ans.end());
    vector<vector<int>> ch(1 << K, vector<int>(2, -1));
    for (int i = 1; i < int(ans.size()); ++i) {
      insert(ans[i], ans[0], ch);
    }
    int d = max_dpt(ans[0], ch);
    if (K <= d and d <= 3 * K) break;
  }
  for (int i = 0; i < int(ans.size()); ++i) {
    cout << ans[i] + 1 << " \n"[i + 1 == int(ans.size())];
  }
  return 0;
}
0