PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000157. Đếm đoạn có đúng K số âm (Count Subarrays With Exactly K Negative Elements)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đếm đoạn có đúng K số âm (Count Subarrays With Exactly K Negative Elements)
Count Subarrays With Exactly K Negative Elements
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given integers and , count nonempty contiguous intervals containing exactly strictly negative elements. Zero is not negative.
Input
The first line contains integers . The second line contains integers .
Output
Print one integer: the number of qualifying intervals.
Subtasks
- Subtask 1 (20 points): ; for all ; .
- Subtask 2 (30 points): ; for all ; .
- Subtask 3 (50 points): ; for all ; .
Example 1
Input
4 1
-1 0 -2 3
Output
6
Explanation
The qualifying intervals are [1,1], [1,2], [2,3], [2,4], [3,3], [3,4]. Each contains exactly one negative number.
Example 2
Input
4 0
0 2 -1 0
Output
4
Explanation
The maximal nonnegative runs are [1,2] and [4,4], contributing 3 and 1 substrings, respectively.