Cleaning
InterviewTime limit2sMemory limit1024 MB
For each window of K consecutive zones, visit them from highest to lowest priority and sum the moves between them. Output the minimum total.
- Level
Medium6 of 10
- Topics
- Sliding window, Segment tree, Array
- Solved
- No attempts yet
Problem
Junseok works for a cleaning company. The area he cleans is a line of N zones numbered 1 through N in order. Zone i is adjacent to zones i-1 and i+1. Moving between two adjacent zones costs a distance of 1.
Each zone has a priority. The priority of zone i is an integer from 1 to N, and a larger value means a higher priority. No two zones have the same priority.
Today Junseok wants to clean K of these zones first. The K zones he cleans must be consecutive. Within the chosen zones, he cleans them in order from highest priority to lowest.
Moving between distant zones takes a long time, so Junseok chooses K consecutive zones that minimize the total moving distance. Output the total moving distance for the best choice.
Input
The first line contains N and K, separated by a space. The second line contains the priorities , separated by spaces.
Output
Print the minimum total moving distance over all possible choices of K consecutive zones.
Constraints
, , and if then .