PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000264. Số lần lật K bit liên tiếp ít nhất (Minimum Number of K Consecutive Bit Flips)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Số lần lật K bit liên tiếp ít nhất (Minimum Number of K Consecutive Bit Flips)
Minimum Number of K Consecutive Bit Flips
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given binary values and an integer , one operation toggles all bits of a contiguous block of exactly elements. Find the minimum number of operations to obtain all ones, or if impossible.
Input
First line: . Second line: exactly integers equal to 0 or 1.
Output
Print the minimum operation count, or -1.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; ; .
Example 1
Input
3 1
0 1 0
Output
2
Explanation
Flip the first and third bits separately.
Example 2
Input
3 2
1 1 0
Output
-1
Explanation
The first two bits force no flips starting there; the final zero cannot start a length-two flip.
Example 3
Input
8 3
0 0 0 1 0 1 1 0
Output
3
Explanation
Flip zero-based blocks 0..2, 4..6, 5..7; successive arrays are 11110110, 11111000, 11111111.