結果

問題 No.566 だいたい完全二分木
ユーザー naimonon77naimonon77
提出日時 2017-09-09 22:42:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 5,107 bytes
コンパイル時間 2,101 ms
コンパイル使用メモリ 180,324 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-07 10:17:07
合計ジャッジ時間 2,819 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define TEST_MODE true

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (a)-1; i >= (int)b; --i)
#define range(i, a, b, c) for (int i = a;             \
                               c > 0 ? i < b : i > b; \
                               i += c)
#define chmax(a, b) (a = (a) < (b) ? (b) : (a))
#define chmin(a, b) (a = (a) > (b) ? (b) : (a))
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(a) begin(a), end(a)
#define ifnot(a) if (not(a))
#define int long long

#ifdef LOCAL_ENV

#if TEST_MODE == true
const bool test = true;
#define dump(x) cerr << #x << " = " << (x) << endl
#else
const bool test = false;
#define dump(x) 
#endif

#else
const bool test = false;
#define dump(x) 
#endif

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int INF = (int)1 << 60;
const ll INFL = (ll)1 << 60;
ll mod_n = (int)1e9 + 7;
const double eps = 1e-10;
typedef long double Real;
// return -1, 0, 1
int sgn(const Real &r) { return (r > eps) - (r < -eps); }
int sgn(const Real &a, const Real &b) { return sgn(a - b); }

//.....................
const int MAX = (int)2e5 + 5;

vector<string> split(const string &str, char sep)
{
    vector<string> v;
    stringstream ss(str);
    string buffer;
    while (getline(ss, buffer, sep))
    {
        v.push_back(buffer);
    }
    return v;
}

template <class InputIterator>
int sum(InputIterator begin, InputIterator end)
{
    return accumulate(begin, end, 0ll);
}

template<typename T>
T gcd(T a, T b)
{
	T c;
	while (a != 0) {
		c = a; a = b%a;  b = c;
	}
	return b;
}

void solve();

struct Node {
    Node* l;
    Node* r;
    int v;
    Node(int v) : l(NULL), r(NULL), v(v) {}

    void print() {
        if (l != NULL) l->print();
        cerr << v << " ";
        if (r != NULL) r->print();
    }

    int height() {
        int tmp = -1;
        if (l != NULL) tmp = max(tmp, l->height());
        if (r != NULL) tmp = max(tmp, r->height());
        return tmp + 1;
    }
    
    void free() {
        if (l != NULL) {
            l->free();
            delete l;
        }
        if (r != NULL) {
            r->free();
            delete r;
        }
    }
};

class Solver {
public:
    int K;

    Node* make_tree(const vector<int>& vec) {
        Node* root = new Node(vec[0]);
        rep2(i, 1, vec.size()) {
            Node* cur = root;
            while (true) {
                if (vec[i] < cur->v) {
                    if (cur->l == NULL) {
                        cur->l = new Node(vec[i]);
                        break;
                    }
                    else cur = cur->l;
                }
                if (vec[i] > cur->v) {
                    if (cur->r == NULL) {
                        cur->r = new Node(vec[i]);
                        break;
                    }
                    else cur = cur->r;
                }
            }
        }
        return root;
    }

    void solve() {
        cin >> K;
        vector<int> vec((1 << K) - 1);
        rep(i, vec.size()) vec[i] = i + 1;
        while (true) {
            rep(i, 100) {
                int a = rand() % vec.size();
                int b = rand() % vec.size();
                if (a == b) continue;
                swap(vec[a], vec[b]);
            }

            // cout << vec[0];
            // rep2(i, 1, vec.size()) cout << " " << vec[i];
            // cout << endl;
            // return;

            Node* root = make_tree(vec);
            int tmp_height = root->height();
            if (K <= tmp_height && tmp_height <= 3 * K) {
                root->print();
                root->free();
                cerr << endl;
                cerr << endl;

                cout << vec[0];
                rep2(i, 1, vec.size()) cout << " " << vec[i];
                cout << endl; 
                return;
            }
            root->free();
            delete root;
        }
    }
};

signed main()
{
    srand(time(NULL));
    cout << fixed << setprecision(20);
    auto ptr = new Solver();
    ptr->solve();
    delete ptr;
    // while (true) {    
    //     auto ptr = new Solver();
    //     ptr->solve();
    //     delete ptr;
    // }
    return 0;
}

// class Mycin {
// 	bool flag = true;
// public:
// 	Mycin& operator >> (int& a) {flag = scanf("%lld", &a) != EOF; return *this;}
// 	Mycin& operator >> (char& a) {flag = scanf("%c", &a) != EOF; return *this;}
// 	Mycin& operator >> (string& s) {flag = (bool)(cin >> s); return *this;}
// 	operator bool() {return flag;}
// } mycin;
 
// class Mycout {
// public:
// 	Mycout& operator << (const int& a) {printf("%lld", a); return *this;}
// 	Mycout& operator << (const char c) {printf("%c", c); return *this;}
// 	Mycout& operator << (const string& s) {printf("%s", s.c_str()); return *this;}
// } mycout;
 
// #define cin mycin
// #define cout mycout
// #define endl '\n'
0