結果

問題 No.3134 二分探索木
ユーザー ID 21712
提出日時 2025-05-02 12:27:07
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,081 ms / 2,000 ms
コード長 1,234 bytes
コンパイル時間 2,918 ms
コンパイル使用メモリ 80,864 KB
実行使用メモリ 103,112 KB
最終ジャッジ日時 2025-06-18 04:59:05
合計ジャッジ時間 17,369 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.lang.*;
import java.util.*;

class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		int N = Integer.parseInt(in.readLine());
		
		int[] B = new int[N], C = new int[N];
		
		C[0] = N - 1;
		
		TreeMap<Integer,Integer> map = new TreeMap<>();
		
		String[] nums = in.readLine().split(" ");
		for (int i = 0; i < N; i++) {
			int a = Integer.parseInt(nums[i]);
			Integer lb = map.lowerKey(a);
			Integer ub = map.higherKey(a);
			if (lb == null) {
				if (ub != null) {
					B[i] = B[map.get(ub)] + 1;
					C[i] = (ub - 1) - 1;
				}
			} else if (ub == null) {
				B[i] = B[map.get(lb)] + 1;
				C[i] = N - (lb + 1);
			} else {
				B[i] = Math.max(B[map.get(lb)], B[map.get(ub)]) + 1;
				C[i] = (ub - 1) - (lb + 1);
			}
			map.put(a, i);
		}
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PrintStream out = new PrintStream(baos);
		
		for (int i = 0; i < N; i++) {
			if (i > 0) out.print(" ");
			out.print(B[i]);
		}
		out.println();

		for (int i = 0; i < N; i++) {
			if (i > 0) out.print(" ");
			out.print(C[i]);
		}
		out.println();
		
		System.out.write(baos.toByteArray());
	}
}
0