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

    solution

    Đề bài: Compute the variance of an array using multithreading

    Given an array of N integers, compute the variance using the formula:

    variance = (sumSq / N) - (sum / N)^2
    
    1. Define a class VarThread extends Thread holding localSum and localSumSq (both long).
    2. Each thread accumulates sum and sum of squares over its segment.
    3. After join(), compute the variance with the formula above (in double).
    4. Print the variance with exactly 4 decimal places (US locale, dot decimal).
    • Định dạng đầu vào:
      • Line 1: N (1 ≤ N ≤ 10⁶)
      • Line 2: N integers
      • Line 3: K (number of threads)
    • Ràng buộc đầu vào:

      |value| ≤ 10⁴

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

      A single line: the variance (4 decimal places).

    Ví dụ:

    Đầu vào:

    5
    1 2 3 4 5
    2
    

    Đầu ra:

    2.0000
    

    Giải thích:

    Mean=3, variance = (1+4+9+16+25)/5 - 9 = 11 - 9 = 2.0000.

    Đầu vào:

    4
    10 10 10 10
    2
    

    Đầu ra:

    0.0000
    

    Giải thích:

    Mảng cùng giá trị → variance = 0.0000.

    Đầu vào:

    1
    7
    1
    

    Đầu ra:

    0.0000
    

    Giải thích:

    Một phần tử → variance = 0.

    Đầu vào:

    6
    1 1 1 1 1 7
    3
    

    Đầu ra:

    5.0000
    

    Giải thích:

    Variance > 0.

    Đang tải editor...