結果

問題 No.566 だいたい完全二分木
ユーザー face4face4
提出日時 2018-06-03 18:44:51
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 720 bytes
コンパイル時間 569 ms
コンパイル使用メモリ 73,880 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-12 21:56:39
合計ジャッジ時間 1,413 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

vector<int> ans;
vector<int> nodes;

void constructTree(int l, int r){
    if(r-l < 2){
        ans.push_back(nodes[l]);
        if(r != l)  ans.push_back(nodes[r]);
        return;
    }

    int mid = (l+r)/2;
    ans.push_back(nodes[mid]);    
    constructTree(l, mid-1);
    constructTree(mid+1, r);
}

int main(){
    int k;
    cin >> k;
    
    int n = pow(2,k)-1;
    ans.push_back(n);

    for(int i = 1; i < n; i++){
        nodes.push_back(i);
    }

    // arguments are indices
    constructTree(0, n-2);

    for(int i = 0; i < n; i++){
        if(i)   cout << " ";
        cout << ans[i];
    }
    cout << endl;

    return 0;
}
0