Problem E
Pinned Files
You recently discovered a new feature in Visual Studio — pinned files! You have no idea what pinned files are good for — you could hear a pin drop when you asked your friends about it — but that doesn’t stop you from pinning and unpinning files all day and night.
Visual Studio maintains a list of the files you currently have open. The list starts with the pinned files and is followed by the unpinned files. Changing the order of the list is done not by click-and-drag operations but by toggling whether a file is pinned or not. If a pinned file is unpinned it is removed from the list and then added before the first unpinned file. If an unpinned file is pinned it is removed and then added after the last pinned file.
Consider the following example with four files labeled
|
|
|
|
Initial State ( |
|
|
|
|
Pin file |
|
|
|
|
Pin file |
|
|
|
|
Unpin file |
|
|
|
|
Unpin file |
|
|
|
|
Unpin file |
These five operations are the minimum number to get from the starting configuration to the ending configuration.
Given the initial list of files, determine the minimum
number of moves to reach a different ordering of the list. The
files are conveniently numbered
Input
Input consists of four lines. The first line contains two
non-negative integers,
Output
Output the minimum number of moves to get from the starting configuration to the ending configuration.
Sample Input 1 | Sample Output 1 |
---|---|
1 1 1 2 1 1 2 1 |
2 |
Sample Input 2 | Sample Output 2 |
---|---|
1 1 1 2 1 1 1 2 |
0 |
Sample Input 3 | Sample Output 3 |
---|---|
2 2 2 1 4 3 1 3 4 2 3 1 |
5 |