結果

問題 No.566 だいたい完全二分木
ユーザー YamaKasaYamaKasa
提出日時 2018-09-25 01:08:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 1,003 bytes
コンパイル時間 3,820 ms
コンパイル使用メモリ 79,008 KB
実行使用メモリ 57,972 KB
最終ジャッジ日時 2023-10-24 23:53:21
合計ジャッジ時間 5,601 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 137 ms
57,604 KB
testcase_01 AC 144 ms
57,736 KB
testcase_02 AC 144 ms
57,680 KB
testcase_03 AC 149 ms
57,724 KB
testcase_04 AC 151 ms
57,500 KB
testcase_05 AC 160 ms
57,576 KB
testcase_06 AC 173 ms
57,972 KB
testcase_07 AC 175 ms
57,952 KB
testcase_08 AC 186 ms
57,560 KB
testcase_09 AC 203 ms
57,944 KB
testcase_10 AC 221 ms
57,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int K = scan.nextInt();
		scan.close();
		if(K == 2) {
			System.out.println("1 3 2");
			System.exit(0);
		}
		int a = (int)Math.pow(2, K) - 1;
		int[]n = new int[a];
		n[0] = (int)Math.pow(2,K - 1);

		int t = 2;
		int r = 2;
		int l = 1;
		int idx = 0;
		int idx2 = 0;
		for(int i = 1; i < a; i++) {
			if(i < r) {
				if(i == l) {
					n[i] = n[idx] / 2;
					idx2 = idx;
					idx = i;
					l = l + t;
				}else {
					n[i] = n[i - 1] + n[idx2];

				}
			}else if(i == r) {
				n[i] = n[i - 1] + n[idx2];
				t = t * 2;
				r = r + t;
			}
		}

		int t1 = (int)Math.pow(2, K - 2) - 1;
		int t2 = (int)Math.pow(2, K - 1) - 1;
		int t3 = t2 + 1;
		int b1 = n[t1];
		int b2 = n[t2];
		int b3 = n[t3];
		n[t1] = b3;
		n[t3] = b1;
		for(int i = 0; i < a; i++) {
			if(i == a - 1) {
				System.out.println(n[i]);
			}else {
				System.out.print(n[i] + " ");
			}
		}

	}
}
0