結果

問題 No.566 だいたい完全二分木
ユーザー uafr_csuafr_cs
提出日時 2017-10-29 04:09:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 964 bytes
コンパイル時間 2,493 ms
コンパイル使用メモリ 78,680 KB
実行使用メモリ 54,356 KB
最終ジャッジ日時 2024-05-01 21:46:04
合計ジャッジ時間 4,649 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
52,952 KB
testcase_01 AC 140 ms
54,132 KB
testcase_02 AC 139 ms
54,052 KB
testcase_03 AC 125 ms
53,780 KB
testcase_04 AC 126 ms
53,980 KB
testcase_05 AC 126 ms
54,044 KB
testcase_06 AC 136 ms
54,024 KB
testcase_07 AC 147 ms
54,084 KB
testcase_08 AC 153 ms
54,032 KB
testcase_09 AC 137 ms
54,356 KB
testcase_10 AC 127 ms
53,184 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