結果

問題 No.566 だいたい完全二分木
ユーザー tottoripapertottoripaper
提出日時 2018-02-22 15:41:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,500 bytes
コンパイル時間 1,966 ms
コンパイル使用メモリ 177,504 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2024-04-08 22:16:27
合計ジャッジ時間 2,755 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

vector<int> v;

struct Tree{
    shared_ptr<Tree> left;
    int v;
    shared_ptr<Tree> right;

    Tree(shared_ptr<Tree> left, int v, shared_ptr<Tree> right)
        : left(left), v(v), right(right) {}
};

void add(shared_ptr<Tree> &t, int v){
    if(t){
        if(v < t->v){
            add(t->left, v);
        }else{
            add(t->right, v);
        }
    }else{
        t = make_shared<Tree>(nullptr, v, nullptr);
    }
}

int height(shared_ptr<Tree> t){
    if(t){
        return max(height(t->left), height(t->right)) + 1;
    }

    return 0;
}

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    int K;
    std::cin >> K;

    v.resize((1 << K) - 1);
    iota(v.begin(), v.end(), 1);
    
    random_device rnd;
    mt19937 engine(rnd());
    while(true){
        shuffle(v.begin(), v.end(), engine);

        shared_ptr<Tree> t = nullptr;
        for(int x : v){
            add(t, x);
        }
        
        int h = height(t);
        if(K <= h && h <= 3 * K){
            for(int i=0;i<v.size();++i){
                std::cout << v[i] << " \n"[i+1 == v.size()];
            }
            break;
        }
    }
}
0