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: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using static System.Convert;
using static System.Math;
using static Constants;
using static Extensions;
using static MathExtensions;

public class Program
{
    public static void Solve()
    {
        var n = I;
        var p = Ls(n);
        var q = new long[n];

        foreach (var i in Range(n))
            q[p[i] - 1] = i;

        var cnt = 1;
        var max = 1;
        foreach (var i in Range(1, n))
        {
            if (q[i - 1] < q[i]) cnt++;
            else cnt = 1;
            max = Max(max, cnt);
        }

        var ans = n - cnt;
        Console.WriteLine(ans);
    }

    #region Scanners
    static TextScanner _ts;

    static char C => char.Parse(_ts.Next());
    static string S => _ts.Next();
    static int I => int.Parse(_ts.Next());
    static long L => long.Parse(_ts.Next());
    static BigInteger B => BigInteger.Parse(_ts.Next());
    static double D => double.Parse(_ts.Next());

    static char[] Cs(int count) => Repeat(count, () => C).ToArray();
    static string[] Ss(int count) => Repeat(count, () => S).ToArray();
    static int[] Is(int count) => Repeat(count, () => I).ToArray();
    static long[] Ls(int count) => Repeat(count, () => L).ToArray();
    static BigInteger[] Bs(int count) => Repeat(count, () => B).ToArray();
    static double[] Ds(int count) => Repeat(count, () => D).ToArray();
    #endregion

    [DebuggerStepThrough]
    public static void Main()
    {
        var sw = new StreamWriter(Console.OpenStandardOutput());
        sw.NewLine = "\n";
#if DEBUG
        sw.AutoFlush = true;
#else
        sw.AutoFlush = false;
#endif
        Console.SetOut(sw);
        _ts = new TextScanner(Console.In);
        Solve();
        Console.Out.Flush();
    }
}

public static class BinarySearch
{
    public static int LowerBound<T>(this IReadOnlyList<T> source, T value)
        where T : IComparable<T>
        => source.LowerBound(value, Comparer<T>.Default);

    public static int LowerBound<T>(this IReadOnlyList<T> source, T value, IComparer<T> comparer)
        => LowerBound(i => source[i], 0, source.Count - 1, value, comparer);

    public static int LowerBound<T>(Func<int, T> func, int minValue, int maxValue, T value)
        where T : IComparable<T>
        => LowerBound(func, minValue, maxValue, value, Comparer<T>.Default);

    public static long LowerBound<T>(Func<long, T> func, long minValue, long maxValue, T value)
        where T : IComparable<T>
        => LowerBound(func, minValue, maxValue, value, Comparer<T>.Default);

    public static BigInteger LowerBound<T>(Func<BigInteger, T> func, BigInteger minValue, BigInteger maxValue, T value)
        where T : IComparable<T>
        => LowerBound(func, minValue, maxValue, value, Comparer<T>.Default);

    public static int LowerBound<T>(Func<int, T> func, int minValue, int maxValue, T value, IComparer<T> comparer)
    {
        var i = Search(x => comparer.Compare(func(x), value) >= 0, minValue, maxValue);
        return comparer.Compare(func(i), value) < 0 ? i + 1 : i;
    }

    public static long LowerBound<T>(Func<long, T> func, long minValue, long maxValue, T value, IComparer<T> comparer)
    {
        var i = Search(x => comparer.Compare(func(x), value) >= 0, minValue, maxValue);
        return comparer.Compare(func(i), value) < 0 ? i + 1 : i;
    }

    public static BigInteger LowerBound<T>(Func<BigInteger, T> func, BigInteger minValue, BigInteger maxValue, T value, IComparer<T> comparer)
    {
        var i = Search(x => comparer.Compare(func(x), value) >= 0, minValue, maxValue);
        return comparer.Compare(func(i), value) < 0 ? i + 1 : i;
    }

    public static int UpperBound<T>(this IReadOnlyList<T> source, T value)
        where T : IComparable<T>
        => source.UpperBound(value, Comparer<T>.Default);

    public static int UpperBound<T>(this IReadOnlyList<T> source, T value, IComparer<T> comparer)
        => UpperBound(i => source[i], 0, source.Count - 1, value, comparer);

    public static int UpperBound<T>(Func<int, T> func, int minValue, int maxValue, T value)
        where T : IComparable<T>
        => UpperBound(func, minValue, maxValue, value, Comparer<T>.Default);

    public static long UpperBound<T>(Func<long, T> func, long minValue, long maxValue, T value)
        where T : IComparable<T>
        => UpperBound(func, minValue, maxValue, value, Comparer<T>.Default);

    public static BigInteger UpperBound<T>(Func<BigInteger, T> func, BigInteger minValue, BigInteger maxValue, T value)
        where T : IComparable<T>
        => UpperBound(func, minValue, maxValue, value, Comparer<T>.Default);

    public static int UpperBound<T>(Func<int, T> func, int minValue, int maxValue, T value, IComparer<T> comparer)
    {
        var i = Search(x => comparer.Compare(func(x), value) > 0, minValue, maxValue);
        return comparer.Compare(func(i), value) <= 0 ? i + 1 : i;
    }

    public static long UpperBound<T>(Func<long, T> func, long minValue, long maxValue, T value, IComparer<T> comparer)
    {
        var i = Search(x => comparer.Compare(func(x), value) > 0, minValue, maxValue);
        return comparer.Compare(func(i), value) <= 0 ? i + 1 : i;
    }

    public static BigInteger UpperBound<T>(Func<BigInteger, T> func, BigInteger minValue, BigInteger maxValue, T value, IComparer<T> comparer)
    {
        var i = Search(x => comparer.Compare(func(x), value) > 0, minValue, maxValue);
        return comparer.Compare(func(i), value) <= 0 ? i + 1 : i;
    }

    public static int Search(Func<int, bool> predicate, int minValue, int maxValue)
    {
        var ok = maxValue;
        var ng = minValue - 1;

        while (System.Math.Abs(ok - ng) > 1)
        {
            var mid = (ok + ng) / 2;
            if (predicate(mid)) ok = mid;
            else ng = mid;
        }

        return ok;
    }

    public static long Search(Func<long, bool> predicate, long minValue, long maxValue)
    {
        var ok = maxValue;
        var ng = minValue - 1;

        while (System.Math.Abs(ok - ng) > 1)
        {
            var mid = (ok + ng) / 2;
            if (predicate(mid)) ok = mid;
            else ng = mid;
        }

        return ok;
    }

    public static BigInteger Search(Func<BigInteger, bool> predicate, BigInteger minValue, BigInteger maxValue)
    {
        var ok = maxValue;
        var ng = minValue - 1;

        while (BigInteger.Abs(ok - ng) > 1)
        {
            var mid = (ok + ng) / 2;
            if (predicate(mid)) ok = mid;
            else ng = mid;
        }

        return ok;
    }
}

[DebuggerStepThrough]
public class TextScanner
{
    private readonly TextReader _tr;

    public TextScanner(TextReader tr)
    {
        _tr = tr;
    }

    public string Next()
    {
        var sb = new StringBuilder();
        int i;
        do
        {
            i = _tr.Read();
            if (i == -1) throw new EndOfStreamException();
        }
        while (char.IsWhiteSpace((char)i));
        while (i != -1 && !char.IsWhiteSpace((char)i))
        {
            sb.Append((char)i);
            i = _tr.Read();
        }
        return sb.ToString();
    }
}

public static class Constants
{
    public const int AnswerToLifeTheUniverseAndEverything = 42;
    public const string Yes = "Yes";
    public const string No = "No";
    public const string YES = "YES";
    public const string NO = "NO";
    public const string Possible = "Possible";
    public const string Impossible = "Impossible";
    public const string POSSIBLE = "POSSIBLE";
    public const string IMPOSSIBLE = "IMPOSSIBLE";
}

[DebuggerStepThrough]
public static class Extensions
{
    public static void Answer(object value)
    {
        Console.WriteLine(value);
        Exit(0);
    }

    public static void Assert(bool condition)
    {
        if (!condition) throw new Exception("Assertion failed");
    }

    public static string AsString(this IEnumerable<char> source) => new string(source.ToArray());

    public static Dictionary<T, int> Bucket<T>(this IEnumerable<T> source) where T : IEquatable<T>
    {
        var dict = new Dictionary<T, int>();
        foreach (var item in source) if (dict.ContainsKey(item)) dict[item]++; else dict[item] = 1;
        return dict;
    }

    public static int[] Bucket<T>(this IEnumerable<T> source, int maxValue, Func<T, int> selector)
    {
        var arr = new int[maxValue + 1];
        foreach (var item in source) arr[selector(item)]++;
        return arr;
    }

    public static IComparer<T> CreateDescendingComparer<T>()
        where T : IComparable<T>
        => Comparer<T>.Create((x, y) => y.CompareTo(x));

    public static IEnumerable<int> CumSum(this IEnumerable<int> source)
    {
        var sum = 0;
        foreach (var item in source) yield return sum += item;
    }

    public static IEnumerable<long> CumSum(this IEnumerable<long> source)
    {
        var sum = 0L;
        foreach (var item in source) yield return sum += item;
    }

    public static void Exit(int exitCode)
    {
        Console.Out.Flush();
        Environment.Exit(exitCode);
    }

    public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
    {
        foreach (var item in source) action(item);
    }

    public static void ForEach<T, _>(this IEnumerable<T> source, Func<T, _> func)
    {
        foreach (var item in source) func(item);
    }

    public static void ForEach<T>(this IEnumerable<T> source, Action<T, int> action)
    {
        var i = 0;
        foreach (var item in source) action(item, i++);
    }

    public static void ForEach<T, _>(this IEnumerable<T> source, Func<T, int, _> func)
    {
        var i = 0;
        foreach (var item in source) func(item, i++);
    }

    // [l, r)
    public static bool IsIn<T>(this T value, T l, T r)
        where T : IComparable<T>
    {
        if (l.CompareTo(r) > 0) throw new ArgumentException();
        return l.CompareTo(value) <= 0 && value.CompareTo(r) < 0;
    }

    public static IEnumerable<int> Range(int start, int end, int step = 1)
    {
        for (var i = start; i < end; i += step) yield return i;
    }

    public static IEnumerable<int> Range(int end) => Range(0, end);

    public static void Repeat(int count, Action action)
    {
        for (var i = 0; i < count; i++) action();
    }

    public static void Repeat(int count, Action<int> action)
    {
        for (var i = 0; i < count; i++) action(i);
    }

    public static IEnumerable<T> Repeat<T>(int count, Func<T> func)
    {
        for (var i = 0; i < count; i++) yield return func();
    }

    public static IEnumerable<T> Repeat<T>(int count, Func<int, T> func)
    {
        for (var i = 0; i < count; i++) yield return func(i);
    }

    public static IEnumerable<int> RangeReverse(int start, int end, int step = 1)
    {
        for (var i = end - 1; i >= start; i -= step) yield return i;
    }

    public static IEnumerable<int> RangeReverse(int end) => RangeReverse(0, end);

    public static void Swap<T>(ref T x, ref T y)
    {
        var tmp = x; x = y; y = tmp;
    }
}

[DebuggerStepThrough]
public static class MathExtensions
{
    public static int DivCeil(int left, int right)
        => left / right + (left % right == 0 ? 0 : 1);

    public static long DivCeil(long left, long right)
        => left / right + (left % right == 0L ? 0L : 1L);

    public static int Gcd(int left, int right)
    {
        int r;
        while ((r = left % right) != 0) { left = right; right = r; }
        return right;
    }

    public static long Gcd(long left, long right)
    {
        long r;
        while ((r = left % right) != 0L) { left = right; right = r; }
        return right;
    }

    public static BigInteger Gcd(BigInteger left, BigInteger right)
        => BigInteger.GreatestCommonDivisor(left, right);

    public static int HighestOneBit(int x)
    {
        x |= x >> 01;
        x |= x >> 02;
        x |= x >> 04;
        x |= x >> 08;
        x |= x >> 16;
        return x - (x >> 1);
    }

    public static long HighestOneBit(long x)
    {
        x |= x >> 01;
        x |= x >> 02;
        x |= x >> 04;
        x |= x >> 08;
        x |= x >> 16;
        x |= x >> 32;
        return x - (x >> 1);
    }

    public static int Lcm(int left, int right) => left / Gcd(left, right) * right;

    public static long Lcm(long left, long right) => left / Gcd(left, right) * right;

    public static BigInteger Lcm(BigInteger left, BigInteger right)
        => left / Gcd(left, right) * right;

    public static int Pow(int value, int exponent)
    {
        var r = 1;
        while (exponent > 0)
        {
            if ((exponent & 1) == 1) r *= value;
            value *= value;
            exponent >>= 1;
        }
        return r;
    }

    public static long Pow(long value, int exponent)
    {
        var r = 1L;
        while (exponent > 0)
        {
            if ((exponent & 1) == 1) r *= value;
            value *= value;
            exponent >>= 1;
        }
        return r;
    }

    public static long Fact(int value)
    {
        var r = 1L;
        for (var i = 2; i <= value; i++) r *= i;
        return r;
    }
}