結果

問題 No.566 だいたい完全二分木
ユーザー 37zigen37zigen
提出日時 2020-03-21 17:19:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 1,259 bytes
コンパイル時間 2,829 ms
コンパイル使用メモリ 77,352 KB
実行使用メモリ 56,960 KB
最終ジャッジ日時 2023-08-24 19:21:44
合計ジャッジ時間 5,075 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,732 KB
testcase_01 AC 121 ms
55,808 KB
testcase_02 AC 123 ms
55,716 KB
testcase_03 AC 127 ms
55,908 KB
testcase_04 AC 128 ms
56,088 KB
testcase_05 AC 132 ms
55,676 KB
testcase_06 AC 146 ms
55,944 KB
testcase_07 AC 163 ms
56,164 KB
testcase_08 AC 180 ms
56,164 KB
testcase_09 AC 192 ms
55,932 KB
testcase_10 AC 222 ms
56,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.FileNotFoundException;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		long t = System.currentTimeMillis();
		new Main().run();
		System.err.println(System.currentTimeMillis() - t);
	}
	
	void preorder(int pos,int[] a) {
		if(pos>=a.length)return;
		ans.addLast(a[pos]);
		preorder(2*pos,a);
		preorder(2*pos+1,a);
	}
	
	//v=その部分木に含まれる最小の数
	//返り値=その部分木に含まれる最大の数
	int dfs(int pos,int v,int res,int[] a) {
		if(res==0) return a[pos]=v;
		a[pos]=dfs(2*pos,v,res-1,a)+1;
		return dfs(2*pos+1,a[pos]+1,res-1,a);
	}
	
	Scanner sc = new Scanner(System.in);
	ArrayDeque<Integer> ans=new ArrayDeque<>();
	
	void run() {
		int K=sc.nextInt();
		int[] a=new int[1<<(K-1)];
		dfs(1,1,K-2,a);
		preorder(1, a);
		ans.addFirst((1<<(K-1)));
		ans.addFirst((1<<(K-1))+1);
		a=new int[1<<(K-1)];
		dfs(1,(1<<(K-1))+2,K-2,a);
		preorder(1, a);
		while(ans.peekLast()>=1<<K)ans.pollLast();
		while(!ans.isEmpty())System.out.print(ans.pollFirst()+(ans.isEmpty()?"\n":" "));
	}
		

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0