QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#874333#3445. Numbers On a Treefernandes_queilaML 81ms44272kbJava171.8kb2025-01-28 01:48:132025-01-28 01:48:13

Judging History

你现在查看的是最新测评结果

  • [2025-01-28 01:48:13]
  • 评测
  • 测评结果:ML
  • 用时:81ms
  • 内存:44272kb
  • [2025-01-28 01:48:13]
  • 提交

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

output:


result: