PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#CC0000041. Đếm đoạn con có trung vị bằng K (Count Subarrays With Median K)
HỌC THEO LỘ TRÌNH
2 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đếm đoạn con có trung vị bằng K (Count Subarrays With Median K)
Count Subarrays With Median K
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Array is a permutation of . Count contiguous subarrays with median equal to . The median is the middle element of the sorted subarray; for even length, use the left middle.
Input
The first line contains . The second line contains a permutation of .
Output
Print the number of subarrays whose median equals .
Subtasks
- Subtask 1 (20 points): ; là hoán vị của ; .
- Subtask 2 (30 points): ; là hoán vị của ; .
- Subtask 3 (50 points): ; là hoán vị của ; .
Examples
Input
5 4
3 2 1 4 5
Output
3
Explanation
Valid subarrays are , and . The median of is 4 because the left middle is used.