Các lượt nộp
    Danh sách bài
    Trang chủ
    Báo lỗi

    solution

    Đề bài: Find the largest perfect square ≤ N in an array using multithreading

    Given an array of N non-negative integers and a value L, find the largest perfect square in the array that does not exceed L. If none exists, print -1.

    1. Define a class SquareFinder extends Thread; each thread holds localBest (default -1).
    2. A number x ≥ 0 is a perfect square if r = (int) Math.sqrt(x) satisfies r*r == x or (r+1)*(r+1) == x.
    3. Each thread scans its segment and updates localBest (only considering values x ≤ L that are perfect squares).
    4. After join(), take the max of all localBest values. Print it.
    • Định dạng đầu vào:
      • Line 1: N (1 ≤ N ≤ 10⁶)
      • Line 2: N non-negative integers
      • Line 3: K (number of threads)
      • Line 4: L (0 ≤ L ≤ 10⁹)
    • Ràng buộc đầu vào:

      0 ≤ values ≤ 10⁹

    • Định dạng đầu ra:

      The largest perfect square ≤ L in the array, or -1.

    Ví dụ:

    Đầu vào:

    6
    1 4 7 9 16 25
    2
    20
    

    Đầu ra:

    16
    

    Giải thích:

    Chính phương ≤ 20: 1, 4, 9, 16 → max = 16.

    Đầu vào:

    4
    3 5 7 11
    2
    100
    

    Đầu ra:

    -1
    

    Giải thích:

    Không số chính phương → -1.

    Đầu vào:

    5
    0 1 4 9 16
    5
    0
    

    Đầu ra:

    0
    

    Giải thích:

    Chỉ 0 là chính phương ≤ 0.

    Đầu vào:

    3
    100 121 144
    2
    130
    

    Đầu ra:

    121
    

    Giải thích:

    100 và 121 ≤ 130; max = 121.

    Đang tải editor...