Problem p04052 - Generation 3

Orig Description

Score : 2000 points
Problem StatementYou are given a permutation P_1 ... P_N of the set {1, 2, ..., N}.
You can apply the following operation to this permutation, any number of times (possibly zero):
Choose two indices i,j (1 ≦ i < j ≦ N), such that j - i ≧ K and |P_i - P_j| = 1.
Then, swap the values of P_i and P_j.
Among all permutations that can be obtained by applying this operation to the given permutation, find the lexicographically smallest one.
Constraints
2≦N≦500,000
1≦K≦N-1
P is a permutation of the set {1, 2, ..., N}.
InputThe input is given from Standard Input in the following format:
N K
P_1 P_2 ... P_N
OutputPrint the lexicographically smallest permutation that can be obtained.
Sample Input 14 2
4 2 3 1
Sample Output 12
1
4
3
One possible way to obtain the lexicographically smallest permutation is shown below:
4 2 3 1
4 1 3 2
3 1 4 2
2 1 4 3
Sample Input 25 1
5 4 3 2 1
Sample Output 21
2
3
4
5
Sample Input 38 3
4 5 7 8 3 1 2 6
Sample Output 31
2
6
7
5
3
4
8

Extracted Specification

An integer N (2 ≤ N ≤ 500,000), representing some quantity or size.
An integer K (1 ≤ K ≤ N-1).
A list of integers P of size N, where P is a permutation of the set {1, 2, ..., N}.

### Example Input:

```
4
2
4 2 3 1
```

### Function Signature:
Write a function f(N, K, P) that takes in the input.
def f(N: int, K: int, P: List[int]):
    ''' 
    N: an integer
    K: an integer
    P: a list of integers
    '''

Test Case Generator

Sure, I can help you write a test case generator for the given problem statement. Here is the Python code for generating test cases for the problem:

```python
import random

def generate_permutation(n):
    permutation = list(range(1, n + 1))
    random.shuffle(permutation)
    return permutation

def tcgen():
    N = random.randint(2, 500000)
    K = random.randint(1, N - 1)
    P = generate_permutation(N)
    
    return N, K, P

# Example of generating a single test case
N, K, P = tcgen()
print(N)
print(K)
print(' '.join(map(str, P)))
```

### Explanation:

1. **generate_permutation(n)**: This helper function creates a random permutation of the set {1, 2, ..., N} by shuffling a list of integers from 1 to N.

2. **tcgen()**: This function generates a random test case:
   - It generates a random integer \(N\) within the range [2, 500,000].
   - It generates a random integer \(K\) within the range [1, N-1].
   - It generates a random permutation of the numbers {1, 2, ..., N} using the `generate_permutation` function.
   
3. **Printing the Test Case**: The function then prints \(N\), \(K\), and the permutation \(P\) in the required format.

You can call the `tcgen()` function multiple times to generate multiple test cases. Each call will provide a new random test case adhering to the constraints specified in the problem statement.

This should give you a comprehensive set of test cases to work with for this competitive programming problem.

Extract Arguments

def extract_arguments(fh):
    N, K = map(int, fh.readline().strip().split())
    P = list(map(int, fh.readline().strip().split()))
    return N, K, P