Improve Inversions

AtCoder
IDarc180_b
Time2000ms
Memory256MB
Difficulty
You are given a permutation $P=(P_1,P_2,\cdots,P_N)$ of $(1,2,\cdots,N)$. Additionally, you are given an integer $K$. You can perform the following operation zero or more times: * Choose integers $l$ and $r$ ($1 \leq l < r \leq N$). Here, the pair $(l,r)$ must satisfy all of the following conditions: * $K \leq r-l$. * $P_l > P_r$ at the time of the operation. * The pair $(l,r)$ has never been chosen before. * Then, swap the values of $P_l$ and $P_r$. You want to maximize the number of operations. Find one way to achieve this. ## Constraints * $2 \leq N \leq 500$ * $1 \leq K \leq N-1$ * $(P_1,P_2,\cdots,P_N)$ is a permutation of $(1,2,\cdots,N)$. * All input values are integers. ## Input The input is given from Standard Input in the following format: $N$ $K$ $P_1$ $P_2$ $\cdots$ $P_N$ [samples]
Samples
Input #1
3 1
3 2 1
Output #1
3
2 3
1 3
1 2

In this example, the maximum number of operations is $3$. The operations in the sample output proceed as follows:

*   1st operation: Choose $(l,r)=(2,3)$. We have $1 \leq 3-2$ and $P_2 > P_3$, and $(2,3)$ has not been chosen before, so the conditions are satisfied. Swap $P_2$ and $P_3$, resulting in $P=(3,1,2)$.
*   2nd operation: Choose $(l,r)=(1,3)$. We have $1 \leq 3-1$ and $P_1 > P_3$, and $(1,3)$ has not been chosen before, so the conditions are satisfied. Swap $P_1$ and $P_3$, resulting in $P=(2,1,3)$.
*   3rd operation: Choose $(l,r)=(1,2)$. We have $1 \leq 2-1$ and $P_1 > P_2$, and $(1,2)$ has not been chosen before, so the conditions are satisfied. Swap $P_1$ and $P_2$, resulting in $P=(1,2,3)$.
Input #2
5 4
1 4 3 2 5
Output #2
0
Input #3
4 2
4 1 2 3
Output #3
2
1 4
1 3
Input #4
10 5
8 7 6 10 9 3 1 5 2 4
Output #4
15
3 8
2 8
3 10
3 9
1 8
2 10
2 9
2 7
1 10
5 10
1 9
4 10
4 9
1 7
1 6
API Response (JSON)
{
  "problem": {
    "name": "Improve Inversions",
    "description": {
      "content": "You are given a permutation $P=(P_1,P_2,\\cdots,P_N)$ of $(1,2,\\cdots,N)$. Additionally, you are given an integer $K$. You can perform the following operation zero or more times: *   Choose integers $",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "arc180_b"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a permutation $P=(P_1,P_2,\\cdots,P_N)$ of $(1,2,\\cdots,N)$. Additionally, you are given an integer $K$.\nYou can perform the following operation zero or more times:\n\n*   Choose integers $...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments