#P5541. Sleepy Cow Herding

    ID: 18771 Type: Default 1000ms 256MiB

Sleepy Cow Herding

Sleepy Cow Herding

Farmer John has (N) cows located at distinct integer positions on a long, narrow one‐dimensional farm (which can be thought of as a number line). The cows are currently not together, and Farmer John wishes to herd them so that they occupy (N) consecutive integer positions (for example, positions (6, 7, 8, \dots)).

However, the cows are very sleepy and difficult to move. At any moment, only a cow at one of the endpoints (i.e. the cow at the minimum or maximum position) can be moved. When moving an endpoint cow, Farmer John may instruct her to move to any unoccupied integer location provided that, after the move, she is no longer an endpoint. In other words, if the current cow positions (after sorting) are (a_1, a_2, \dots, a_N), then a cow at (a_1) or (a_N) can be moved to a new position (x) if and only if (a_1 < x < a_N) and (x) is not already occupied.

Your task is to compute the minimum and maximum number of moves necessary to group the cows into (N) consecutive positions.

Note: Any formula in the problem statement is written in (\LaTeX) format. For instance, the maximum number of moves can be computed using [ \max\Bigl( a_N - a_2 - (N-2),; a_{N-1} - a_1 - (N-2) \Bigr). ]
And the minimum number of moves can be determined using a sliding window technique with a special case adjustment: [ \text{minMoves} = \min\Bigl( N - \text{(largest number of cows that can already fit in an interval of length } N-1 \Bigr),. ] In the special case where (a_{N-2} - a_1 = N-2) and (a_N - a_{N-1} > 2) (or symmetrically (a_N - a_2 = N-2) and (a_2 - a_1 > 2)), then (\text{minMoves}) is set to 2.

inputFormat

The first line contains a single integer (N) (the number of cows). Each of the next (N) lines contains an integer representing the position of a cow. The positions are distinct and can be given in any order.

outputFormat

Output two lines. The first line should contain the minimum number of moves needed. The second line should contain the maximum number of moves possible.

sample

3
4
7
9
1

2

</p>