#BS0000398. Trung bình đoạn con lớn nhất II (Maximum Average Subarray II)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Trung bình đoạn con lớn nhất II (Maximum Average Subarray II)
Maximum Average Subarray II
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given an integer sequence and an integer , choose a contiguous segment from index through index , where and .
The average of the selected segment is
Determine the greatest average over all valid segments. Values may be negative; no element between the two endpoints may be skipped.
Input
- The first line contains two integers : the sequence length and the minimum segment length.
- The second line contains integers in sequence order.
Output
Print one real number equal to the maximum average. The absolute error must be strictly less than .
Subtasks
- Subtask 1 (20 points): ; for every .
- Subtask 2 (30 points): ; for every .
- Subtask 3 (50 points): ; for every .
Examples
Input
6 4
1 12 -5 -6 50 3
Output
12.7500000000
Explanation
The three length-4 sums are 2, 51 and 42, giving averages 0.5, 12.75 and 10.5. Length-5 sums are 52 and 54, giving 10.4 and 10.8. The full sequence averages 55/6. The maximum is 12.75, at indices 2 through 5.
Input
3 2
9 -10 9
Output
2.6666666667
Explanation
Both length-2 segments sum to -1 and average -0.5. The length-3 segment sums to 8 and averages , which is larger.
Input
1 1
-10000
Output
-10000.0000000000
Explanation
The only eligible segment contains -10000, so its average is -10000.
Input
1 1
5
Output
5.0000000000
Explanation
There is one element, 5, and the minimum length is 1. The only eligible segment has sum 5 and length 1, giving the maximum average .