QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#874333 | #3445. Numbers On a Tree | fernandes_queila | ML | 81ms | 44272kb | Java17 | 1.8kb | 2025-01-28 01:48:13 | 2025-01-28 01:48:13 |
Judging History
answer
import java.util.Scanner;
public class BinarySearchTree {
public static int binarySearch(int[] arr, int target) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
public static int calculateLabel(int height, String path) {
int totalNodes = (int) Math.pow(2, height + 1) - 1;
int label = totalNodes;
int index = 0;
int[] nodes = new int[totalNodes];
for (int i = 0; i < totalNodes; i++) {
nodes[i] = i;
}
for (int i = 0; i < path.length(); i++) {
char move = path.charAt(i);
if (Character.toLowerCase(move) == 'l') {
index = 2 * index + 1;
} else if (Character.toLowerCase(move) == 'r') {
index = 2 * index + 2;
} else {
throw new IllegalArgumentException("Invalid move: use 'L' or 'R'.");
}
}
label -= index;
int searchResult = binarySearch(nodes, label);
return (searchResult != -1) ? searchResult : label;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String inputLine = scanner.nextLine().strip();
String[] parts = inputLine.split(" ");
int height = Integer.parseInt(parts[0]);
String path = parts.length > 1 ? parts[1] : "";
int result = calculateLabel(height, path);
System.out.println(result);
scanner.close();
}
}
详细
Test #1:
score: 100
Accepted
time: 78ms
memory: 44272kb
input:
3 LR
output:
11
result:
ok single line: '11'
Test #2:
score: 0
Accepted
time: 80ms
memory: 44136kb
input:
3 RRL
output:
2
result:
ok single line: '2'
Test #3:
score: 0
Accepted
time: 73ms
memory: 44228kb
input:
2
output:
7
result:
ok single line: '7'
Test #4:
score: 0
Accepted
time: 81ms
memory: 44224kb
input:
3 LRL
output:
6
result:
ok single line: '6'
Test #5:
score: -100
Memory Limit Exceeded
input:
26 RRLLLLRRRL