QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#873923 | #3445. Numbers On a Tree | fernandes_queila | TL | 82ms | 44408kb | Java17 | 1.7kb | 2025-01-27 09:10:48 | 2025-01-27 09:10:50 |
Judging History
answer
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine().trim();
String[] parts = line.split(" ");
int height = Integer.parseInt(parts[0]);
String road = parts.length > 1 ? parts[1] : "";
BalancedTree balancedTree = new BalancedTree();
balancedTree.constructAVL(height);
int result = balancedTree.searchNode(height, road);
System.out.println(result);
scanner.close();
}
}
class TreeNode {
int value;
TreeNode left, right;
TreeNode(int value) {
this.value = value;
this.left = this.right = null;
}
}
class BalancedTree {
private TreeNode root;
public void constructAVL(int height) {
int totalNodes = (1 << (height + 1)) - 1;
root = buildTree(1, totalNodes);
}
private TreeNode buildTree(int start, int end) {
if (start > end) return null;
int mid = (start + end) / 2;
TreeNode node = new TreeNode(mid);
node.left = buildTree(start, mid - 1);
node.right = buildTree(mid + 1, end);
return node;
}
public int searchNode(int height, String road) {
int rootLabel = (1 << (height + 1)) - 1;
if (road.isEmpty()) {
return rootLabel;
}
int label = rootLabel;
int local = 0;
for (char move : road.toCharArray()) {
if (Character.toLowerCase(move) == 'l') {
local = 2 * local + 1;
} else if (Character.toLowerCase(move) == 'r') {
local = 2 * local + 2;
}
}
label -= local;
return label;
}
}
详细
Test #1:
score: 100
Accepted
time: 79ms
memory: 44368kb
input:
3 LR
output:
11
result:
ok single line: '11'
Test #2:
score: 0
Accepted
time: 80ms
memory: 44140kb
input:
3 RRL
output:
2
result:
ok single line: '2'
Test #3:
score: 0
Accepted
time: 82ms
memory: 44160kb
input:
2
output:
7
result:
ok single line: '7'
Test #4:
score: 0
Accepted
time: 82ms
memory: 44408kb
input:
3 LRL
output:
6
result:
ok single line: '6'
Test #5:
score: -100
Time Limit Exceeded
input:
26 RRLLLLRRRL