#P4280. Minimizing Inversion Count in a Partially Hidden Sequence

    ID: 17526 Type: Default 1000ms 256MiB

Minimizing Inversion Count in a Partially Hidden Sequence

Minimizing Inversion Count in a Partially Hidden Sequence

During summer vacation, Xiao Keke and his friends visit the beach and discover a small island called "Happy Island", which is a giant amusement park with many brain‐teasing games. Coincidentally, an event titled "Solve the Puzzle and Win Free Tickets" is being held on the island. The challenge is as follows: Given a sequence of positive integers (each originally chosen from 1 to K), some of the numbers are hidden and marked as -1. The task is to fill the missing positions (with any number between 1 and K) so that the total number of inversion pairs is minimized.

An inversion pair is defined as two numbers A and B such that A appears to the left of B and A > B. For example, in the sequence 4 2 1 3 3 there are 5 inversion pairs: (4,2), (4,1), (4,3), (4,3) and (2,1).

For instance, if a sequence of 5 numbers originally taken from the range [1,4] becomes 4 2 -1 -1 3 after some numbers are hidden, then one possible completion is 4 2 1 3 3 and another is 4 2 4 4 3. Your task is to determine the minimum possible inversion count among all completions.

Note: The inversion count is calculated over all pairs of positions (i, j) with i < j where the number at position i is greater than that at position j.

inputFormat

The first line contains two integers n and K, where n is the length of the sequence and K is the maximum allowed value (the numbers are originally chosen from 1 to K). The second line contains n space‐separated integers. Known numbers are positive integers and missing numbers are represented by -1.

outputFormat

Output a single integer representing the minimum possible inversion count after replacing every -1 with a number in the range [1, K].

sample

5 4
4 2 -1 -1 3
4