#FT0000047. Chi phí cửa sổ trượt (Sliding Window Cost)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chi phí cửa sổ trượt (Sliding Window Cost)
Sliding Window Cost
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
Given integers and window length , consider every contiguous window containing exactly values, from left to right.
For each window, change its values to one common integer by increasing or decreasing individual elements. Changing into costs , and the total cost is the sum over the window. Find the minimum possible total cost for every window. Windows are evaluated independently; hypothetical changes in one window do not alter the original array.
Input
- Line 1: integers , array length and window length.
- Line 2: exactly integers .
Output
Print integers on one line, the minimum costs for each successive window.
Subtasks
Subtask 1 (20 points):
- ; for every .
Subtask 2 (30 points):
- ; for every .
Subtask 3 (50 points):
- ; for every .
Examples
Examples 1
Input
8 3
2 4 3 5 8 1 2 1
Output
2 2 5 7 7 1
Explanation
For , choosing common value costs . Subsequent windows choose medians , yielding costs . A median minimizes total absolute deviations.
Examples 2
Input
4 2
5 5 1 9
Output
0 4 8
Explanation
The values in already agree, so the cost is zero. For , every common value between and gives cost . For , the minimum cost is .
For a pair , choosing a common target between them gives total cost . Thus the windows , , and contribute , , and , respectively.