package test8; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * No.365 ジェンガソート * https://yukicoder.me/problems/no/365 */ public class Question_24_0921 { final static int COUNT_MIN = 1; final static int LENTTH_MIN = 1; final static int COUNT_MAX = 100000; static int LENTTH_MAX; public static void main(String[] args) { InputStreamReader re = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(re); try { int count = Integer.parseInt(br.readLine()); LENTTH_MAX = count; String[] inputStr = br.readLine().split(" "); //int comparativeNum = Integer.parseInt(inputStr[0]); int resultCount = 0; int comparativeNum = count; boolean errorFlg = false; //ArrayList list = new ArrayList(); //list.add(Integer.parseInt(inputStr[0])); //有効値判定 if (NumJudg(count, COUNT_MIN, COUNT_MAX)) { for (int i = inputStr.length - 1; i > 0; i--) { int num = Integer.parseInt(inputStr[i]); if (NumJudg(num, LENTTH_MIN, LENTTH_MAX)) { if (num == comparativeNum) { comparativeNum--; } else { resultCount++; } } else { errorFlg = true; break; } } if (errorFlg) { System.out.println("入力値が有効範囲外です"); } else { System.out.println(resultCount); } } else { System.out.println("入力値が有効範囲外です"); } } catch (NumberFormatException e){ System.out.println("数字を入力して下さい"); } catch (IOException e) { System.out.println("エラーが発生しました"); } finally { try { re.close(); br.close(); } catch (IOException e) { System.out.println("InputStreamReader、BufferedReaderクローズ中にエラーが発生しました"); } } } /** * 有効値判定 * @param input 判定するもの * @param max 最大値 * @param min 最小値 * @return 範囲内ならtrue,範囲外ならfalseを返す */ private static boolean NumJudg(int input, int min, int max) { Boolean result = false; if (min <= input && input <= max) { result = true; } return result; } }