PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FT0000123. Trung bình của các đoạn con (Mean of Array)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Trung bình của các đoạn con (Mean of Array)
Mean of Array
Source: SPOJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given positive integers and a positive integer , count all non-empty contiguous subarrays whose arithmetic mean is not less than (that is, ).
Each pair of start/end positions identifies a distinct subarray, even when its contents match another range. The condition can be evaluated with integers by comparing its sum with times its length, or by subtracting from each element.
Input
- First line: positive integers .
- Second line: positive integers of the array.
Output
Print the number of non-empty contiguous subarrays whose arithmetic mean is at least .
Subtasks
- Subtask 1 (20 points): ; ; ; all data values are positive 32-bit integers.
- Subtask 2 (30 points): ; ; ; all data values are positive 32-bit integers.
- Subtask 3 (50 points): ; ; ; all data values are positive 32-bit integers.
Examples
Example 1
Input
5 4
5 2 4 5 1
Output
5
Explanation
- [1,1]: 5/1 >= 4 → ✓
- [1,2]: 7/2 >= 4 → ✗
- [1,3]: 11/3 >= 4 → ✗
- [1,4]: 16/4 >= 4 → ✓
- [1,5]: 17/5 >= 4 → ✗
- [2,2]: 2/1 >= 4 → ✗
- [2,3]: 6/2 >= 4 → ✗
- [2,4]: 11/3 >= 4 → ✗
- [2,5]: 12/4 >= 4 → ✗
- [3,3]: 4/1 >= 4 → ✓
- [3,4]: 9/2 >= 4 → ✓
- [3,5]: 10/3 >= 4 → ✗
- [4,4]: 5/1 >= 4 → ✓
- [4,5]: 6/2 >= 4 → ✗
- [5,5]: 1/1 >= 4 → ✗
5 valid subarrays in total.
Example 2
Input
4 3
1 5 2 4
Output
7
Explanation
- [1,1]: 1/1 >= 3 → ✗
- [1,2]: 6/2 >= 3 → ✓
- [1,3]: 8/3 >= 3 → ✗
- [1,4]: 12/4 >= 3 → ✓
- [2,2]: 5/1 >= 3 → ✓
- [2,3]: 7/2 >= 3 → ✓
- [2,4]: 11/3 >= 3 → ✓
- [3,3]: 2/1 >= 3 → ✗
- [3,4]: 6/2 >= 3 → ✓
- [4,4]: 4/1 >= 3 → ✓
7 valid subarrays in total.