結果

問題 No.566 だいたい完全二分木
ユーザー vtr_codervtr_coder
提出日時 2017-09-08 23:55:44
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,435 bytes
コンパイル時間 1,886 ms
コンパイル使用メモリ 166,164 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-07 08:41:24
合計ジャッジ時間 2,196 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i,n) for(int i=0;i<n;++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))

#define MOD 1000000007
#define INF 1000000000

int K;
int pow_f(int tmp) {
    int ret = 1;
    REP(i, tmp) { ret *= 2; }
    return ret;
}
vector<int> ans;

// [x, y] の中で真ん中を取り続ける
int dfs(int x, int y) {
    if(x == y) {
        ans.push_back(x);
        return 0;
    }
    int num = y - x + 1;
    if(num % 2 != 0) {
        int push_num = num / 2 + x;
        ans.push_back(push_num);
        dfs(x, push_num - 1);
        dfs(push_num + 1, y);
    } else {
        // 高い方を入れる
        if(num == 2) {
            ans.push_back(y);
            ans.push_back(x);
            return 0;
        } else {
            int push_num = num / 2 + x;
            ans.push_back(push_num);
            dfs(x, push_num - 1);
            dfs(push_num + 1, y);
        }
    }
    return 0;
}

int main()
{
    cin >> K;
    // 真ん中 + 1 をとる
    int root = pow_f(K - 1) + 1;
    ans.push_back(root);
    dfs(1, root - 1);
    if(root + 1 <= pow_f(K) - 1) {
        dfs(root + 1, pow_f(K) - 1);
    }

    REP(i, ans.size()) {
        printf("%d", ans[i]);
        if(ans.size() - 1 != i) { printf(" "); }
        else { printf("\n"); }
    }

    return 0;
}
0