結果

問題 No.566 だいたい完全二分木
ユーザー uafr_csuafr_cs
提出日時 2017-10-29 04:09:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 964 bytes
コンパイル時間 2,246 ms
コンパイル使用メモリ 80,572 KB
実行使用メモリ 56,608 KB
最終ジャッジ日時 2023-08-14 09:09:39
合計ジャッジ時間 4,930 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
55,984 KB
testcase_01 AC 141 ms
56,608 KB
testcase_02 AC 138 ms
56,280 KB
testcase_03 AC 134 ms
55,676 KB
testcase_04 AC 141 ms
55,940 KB
testcase_05 AC 141 ms
56,192 KB
testcase_06 AC 142 ms
56,240 KB
testcase_07 AC 145 ms
56,300 KB
testcase_08 AC 140 ms
55,644 KB
testcase_09 AC 138 ms
55,952 KB
testcase_10 AC 147 ms
55,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
	
	public static void dfs(int begin, int last, StringBuilder sb){
		final int size = (last - begin) + 1;
		if(size <= 0){ return; }
		
		final int curr = begin + size / 2;
		//System.out.println(size + " : " + begin + ", " + last + " : " + curr);
		
		sb.append(" " + (begin + size / 2));
		if(size >= 2){
			dfs(begin, begin + size / 2 - 1, sb);
			dfs(begin + size / 2 + 1, last, sb);
			return;
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int K = sc.nextInt();
		final int limit = (1 << K) - 1;
		
		final int first = limit / 2 + 2;
		StringBuilder sb = new StringBuilder();
		sb.append(first);
		//System.out.println(1 + " " + first + " " + limit);
		
		dfs(1, first - 1, sb);
		if(first < limit){
			dfs(first + 1, limit, sb);
		}
		
		System.out.println(sb.toString());
	}
}
0