P-Networks

Time limit2sMemory limit128 MB

Summary
Given a permutation of N wires, decide whether a p-network can realize it, and if so report the minimum number of strokes needed.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy, Combinatorics, Implementation
Solved
No attempts yet

Problem

Pretty Networks Inc. builds curious artifacts that transform a set of input values in a prescribed way. Each transformation is determined by what they call a p-network.

p-network example

A p-network of order NN and size MM has NN horizontal wires numbered 1,2,…,N1, 2, \ldots, N and MM vertical strokes. Each stroke connects two consecutive wires. No two strokes touch the same point of any wire, and no stroke touches the leftmost or rightmost point of any wire. The picture above shows a p-network of order 55 and size 99.

The transformation determined by a p-network follows from these traversal rules:

  1. start at the leftmost point of one wire and move to the right;
  2. whenever a stroke appears, move to the wire it connects and keep going from left to right;
  3. stop when the rightmost point of a wire is reached.

If starting on wire ii the traversal ends on wire jj, we say the p-network transforms ii into jj, written i→ji \to j. The p-network in the picture realizes the transformations

{1→3,  2→5,  3→4,  4→1,  5→2}.\{1 \to 3,\; 2 \to 5,\; 3 \to 4,\; 4 \to 1,\; 5 \to 2\}.

You are given an order NN and a desired set of transformations {1→i1,  2→i2,  …,  N→iN}\{1 \to i_1,\; 2 \to i_2,\; \ldots,\; N \to i_N\}. Decide whether any p-network of order NN can realize them. When one exists, report the minimum possible size MM, i.e., the smallest number of strokes over all p-networks that realize the transformations. (Such a minimum never exceeds N(N−1)2\frac{N(N-1)}{2}, which is well below the 4N24N^2 bound the company works with.)

Input

The input contains several p-network design problems. Each problem is a single line with the values N,i1,i2,…,iNN, i_1, i_2, \ldots, i_N separated by single spaces. Here NN is the order of the desired p-network, i.e., its number of wires (1≤N≤201 \le N \le 20), and the values i1,i2,…,iNi_1, i_2, \ldots, i_N mean the p-network must realize the transformations {1→i1,  2→i2,  …,  N→iN}\{1 \to i_1,\; 2 \to i_2,\; \ldots,\; N \to i_N\} (1≤ij≤N1 \le i_j \le N for every 1≤j≤N1 \le j \le N). The input ends with a line containing N=0N = 0, which must not be processed.

Output

For each design problem output a single line. If no p-network realizes the requested transformations, the line must be No solution. Otherwise the line must contain a single integer: the minimum size MM (number of strokes) of a p-network of order NN that realizes them.

Examples3

  1. Example 1

    Input
    5 3 5 4 1 2
    3 1 1 3
    2 1 2
    2 1 2
    0
    
    Expected output
    7
    No solution
    0
    0
    
  2. Example 2

    Input
    1 1
    0
    
    Expected output
    0
    
  3. Example 3

    Input
    5 5 4 3 2 1
    0
    
    Expected output
    10