Phoebe's Melody
Practice
4.3 (11 votes)
Approved
Data structures
Dynamic programming
Medium
Open
Problem
84% Success 1207 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Phoebe enjoys playing music. She especially enjoys playing it for her friends.

Phoebe has made a new musical instrument. The instrument is very much like a piano. It has N keys arranged in a straight line, numbered from 1 to N. The ith key has volume Vi. No two keys have the same volume and 1ViN. It takes |i-j| time to move from the ith key to the jth key on the instrument. Phoebe has a unique way of playing music. Immediately after playing key i, she can play only a key j such that:

  • j is not closer than K positions from key i (i.e. j should not be in the range [ i-K+1, i+K-1 ]).

  • Vj < Vi.

Each key may have 0 or more keys that can be played immediately after it.

Phoebe wants to find the summation of time required to go from each key i to the closest key that can be played after it. If there is no next playable key for a key i, then consider its time taken as 0.

Input:

The first line of the input contains T, the number of test cases.
The first line of each test case contains N and K. The second line of each test case contains N integers of the array V.

Output:

For each test case, output a single number denoting the summation of time taken to move from each key i to the closest next playable key after i.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 2 * 105
  • 1 <= K,V[i] <= N

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
7 votes
Tags:
Medium
Points:30
15 votes
Tags:
AlgorithmsDynamic ProgrammingHiringMedium
Points:30
49 votes
Tags:
AlgorithmsDynamic ProgrammingSegment Trees