using System; namespace JengaSort { class Program { static void Main(string[] args) { //ブロックの個数をNで定義 int N = InputN(1, 100000); //それぞれのブロックの長さをEachLength配列で定義 int[] EachLength = Input(N, 1, N); int Result = Processing(N , EachLength); int Answer = N - Result; Console.WriteLine(Answer); } //1行目の入力が数字で行われているか、数値は指定の範囲内のものかを判定 static int InputN(int Min , int Max) { int Number = 0; try { Number = int.Parse(Console.ReadLine()); if (Number < Min || Number > Max) { Console.WriteLine(Min + "以上" + Max + "以下で入力してください"); } }catch(FormatException e) { Console.WriteLine("数字を入力してください"); }catch(Exception E) { Console.WriteLine("想定外のエラーです"); } return Number; } //2行目の入力が数字で行われているか、数値は指定の範囲内のものかを判定 static int[] Input(int N ,int Min , int Max) { String[] Numbers = null; try { Numbers = Console.ReadLine().Split(); for(int i = 0; i < N; i++) { if(int.Parse(Numbers[i]) < Min || int.Parse(Numbers[i]) > Max) { Console.WriteLine(Min + "以上" + Max + "以下の数字で入力してください"); } } } catch(Exception E) { Console.WriteLine("想定外のエラーです"); } //String型配列をint型配列に変換 int[] NewNumbers = new int[N]; for (int j = 0; j < N; j++) { NewNumbers[j] = int.Parse(Numbers[j]); } return NewNumbers; } //移動がない数字の個数を計算するメソッド static int Processing(int N , int[] EachLength) { //変動しない数字の個数をCount変数で数える int Count = 0; //変動する最大を更新して所持する変数 int Match = N; for (int i = N - 1; i >= 0; i--) { if(EachLength[i] == Match) { Count++; Match--; } } return Count; } } }