Order the Horses
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Zenyk and Marichka have \(n\) stallions, numbered from \(1\) to \(n\), which they keep in a stable. There are \(n\) stalls in the stable, arranged in a row, also numbered from \(1\) to \(n\). The horse with number \(i\) has its own stall with the same number \(i\).
One day, after a hard day’s work on the farm, Marichka led the horses into the stable, and poor her, she mixed up the order of the horses. She put the horse with number \(p_i\) into stall \(i\).
Zenyk noticed that something was wrong in the stable. He wants to put the horses back in their places.
You are given a number \(k\).
Zenyk will first divide the horses in the stable into \(k\) segments. Each segment contains horses standing in stalls with numbers from \(l\) to \(r\), that is, horses with numbers \(p_l, p_{l+1}, \dots, p_r\), where \(1 \le l \le r \le n\). Each horse will belong to exactly one segment. After that, Zenyk can rearrange the segments among themselves in any way he wants.
Tell whether Zenyk can rearrange the horses in the correct order, i.e., \(1, 2, \dots, n\)?
Input
The first line contains two integers \(n\) and \(k\) — the number of horses in the stable and the number of segments they can be divided into.
The second line contains \(n\) integers \(p_i\) — the number of the horse standing in the stall with number \(i\).
Output
In a single line, output Yes if Zenyk can rearrange the
horses in the correct order, or No if he cannot.
Constraints
\(1 \le k \le n \le 3 \cdot 10^5\),
\(1 \le p_i \le n\),
all \(p_i\) are distinct.
Scoring consists of the following subtasks:
1 point for each sample from the problem statement,
10 points: \(k = 1\),
15 points: \(k = 2, n \le 200\),
20 points: \(k = 2\),
30 points: \(n \le 200\),
23 points: no additional constraints.
Points for a subtask are awarded only if your program passes all tests in that subtask.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 7 4 5 6 4 1 2 3 7 | Yes |
| Input (stdin) | Output (stdout) |
|---|---|
| 7 3 5 6 4 1 2 3 7 | No |
Notes
Let the horses stand in the stable in the order \((5, 6, 4, 1, 2, 3, 7)\). They can be divided into four segments \((5, 6), (4), (1, 2, 3), (7)\), and these segments can be rearranged: \((1, 2, 3), (4), (5, 6), (7)\). Then the horses will stand in the correct order.
However, it is not possible to divide the horses into three segments to rearrange them correctly.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Order the Horses - Editorial
The task is to find the smallest number of groups the horses must be split into so that they can be put in order. Call this number \(m\). If \(m \le k\), the answer is yes; otherwise it is no.
Consider two horses \(p_i\) and \(p_{i+1}\) standing next to each other.
If \(p_{i+1} = p_i + 1\), these horses will also stand next to each other after ordering, so there is no point in putting them into different groups.
If \(p_{i+1} \ne p_i + 1\), the horses will no longer stand next to each other, so they must be put into different groups.
So to find \(m\), we count the positions \(i\) with \(p_{i+1} \ne p_i + 1\) and add one (if the horses are already in the right order, \(m=1\)).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|