Problem:
Score : 500 points

Problem Statement

You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N.
You would like to sort this sequence in ascending order by repeating the following operation:

Choose an element in the sequence and move it to the beginning or the end of the sequence.

Find the minimum number of operations required.
It can be proved that it is actually possible to sort the sequence using this operation.

Constraints

1 \leq N \leq 2\times 10^5

(P_1,P_2,...,P_N) is a permutation of (1,2,...,N).

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1
:
P_N

Output

Print the minimum number of operations required.

Sample Input 1

4
1
3
2
4

Sample Output 1

2

For example, the sequence can be sorted in ascending order as follows:

Move 2 to the beginning. The sequence is now (2,1,3,4).

Move 1 to the beginning. The sequence is now (1,2,3,4).

Sample Input 2

6
3
2
5
1
4
6

Sample Output 2

4

Sample Input 3

8
6
3
1
2
7
4
8
5

Sample Output 3

5

Solution in C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AGC024_B
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());

            BitArray bitArray = new BitArray(n + 1);
            int[] continuousLengthArray = new int[n + 1];
            int[] continuous = new int[n];
            int continuousIndex = 0;
            int continuousStartIndex = 0;

            int maxContinuousLength = 1;

            int number = int.Parse(Console.ReadLine());
            int startNumber = number;

            bitArray[number] = true;
            //continuousLengthArray[number] |= (int)0x40000000;

            int restNumberCount = n - 1;

            continuous[continuousIndex] = number;
            ++continuousIndex;

            for (int i = 1; i < n; ++i)
            {
                number = int.Parse(Console.ReadLine());

                bitArray[number] = true;
                //continuousLengthArray[number] |= (int)0x40000000;

                --restNumberCount;

                bool found = false;

                for (int j = continuousStartIndex; j < continuousIndex; ++j)
                {
                    var kvp = continuous[j];

                    if (kvp == -1)
                    {
                        if (j == continuousIndex - 1)
                        {
                            --continuousIndex;
                        }
                        if (j == continuousStartIndex)
                        {
                            ++continuousStartIndex;
                        }
#if false
                        else if (continuous[j - 1] != -1)
                        {
                            continuous[j] = continuous[j - 1];
                            continuous[j - 1] = -1;
                        }
#endif
                        continue;
                    }

                    //int continuousLength = (continuousLengthArray[kvp] & (int)0x3fffffff) + 1;
                    int continuousLength = continuousLengthArray[kvp] + 1;
                    int nextNumber = kvp + continuousLength;

                    if (nextNumber == number)
                    {
                        ++continuousLengthArray[kvp];
                        ++continuousLength;

                        if (continuousLength > maxContinuousLength)
                        {
                            maxContinuousLength = continuousLength;
                        }

                        found = true;

                        if (nextNumber + 1 > n ||
                            //(continuousLengthArray[nextNumber + 1] & (int)0x40000000) != 0)
                            bitArray[nextNumber + 1])
                        {
                            continuous[j] = -1;
                            if (j == continuousIndex - 1)
                            {
                                --continuousIndex;
                            }
                            if (j == continuousStartIndex)
                            {
                                ++continuousStartIndex;
                            }
#if false
                            else if (continuous[j - 1] != -1)
                            {
                                continuous[j] = continuous[j - 1];
                                continuous[j - 1] = -1;
                            }
#endif
                        }

                        break;
                    }
#if true
                    else if (kvp + maxContinuousLength > n || bitArray[kvp + maxContinuousLength])
                    {
                        continuous[j] = -1;
                        if (j == continuousIndex - 1)
                        {
                            --continuousIndex;
                        }
                        if (j == continuousStartIndex)
                        {
                            ++continuousStartIndex;
                        }
#if false
                        else if (continuous[j - 1] != -1)
                        {
                            continuous[j] = continuous[j - 1];
                            continuous[j - 1] = -1;
                        }
#endif
                    }
#endif
                }

                if (!found)
                {
                    bool addFlag = true;
                    if (number + maxContinuousLength > n)
                    {
                        addFlag = false;
                    }
                    else if (maxContinuousLength > restNumberCount)
                    {
                        addFlag = false;
                    }
                    else
                    {
#if true
                        //if ((continuousLengthArray[number + 1] & (int)0x40000000) != 0)
                        if (bitArray[number + 1])
                            //if (valueNumberToArrayIndex[number + 1] != 0)
                        {
                            addFlag = false;
                        }
#else
                        for (int nextOffset = 1; nextOffset < maxContinuousLength; ++nextOffset)
                        {
                            if (bitArray[number + nextOffset])
                            //if (valueNumberToArrayIndex[number + nextOffset] != 0)
                            {
                                addFlag = false;
                                break;
                            }
                        }
#endif
                    }
                    if (addFlag)
                    {
                        continuous[continuousIndex] = number;
                        ++continuousIndex;
                    }
                }

            }

#if false
            for (int j = 0; j < continuousIndex; ++j)
            {
                var kvp = continuous[j];

                if (kvp == -1) continue;

                int continuousLength = continuousLengthArray[kvp] + 1;
                if (continuousLength > maxContinuousLength)
                {
                    maxContinuousLength = continuousLength;
                }

            }
#endif

            int count = n - maxContinuousLength;
            Console.WriteLine("{0}", count);
        }
    }
}