using System; using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Text.RegularExpressions; namespace Exr { class Program { static void Main(string[] args) { int[] nums = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); while (true) { string input = Console.ReadLine(); if(input == "end") { PrintResult(nums); break; } ExecuteCommand(input, nums); } } private static void ExecuteCommand(string input, int[] nums) { string[] tokens = input.Split(" ", StringSplitOptions.RemoveEmptyEntries); switch (tokens[0]) { case "exchange": int index = int.Parse(tokens[1]); if(index < 0 || index >= nums.Length) { Console.WriteLine("Invalid index"); } else { ExchangeArray(index, nums); } break; case "max": int maxResult = GetMaxElement(tokens[1], nums); if(maxResult == -1) { Console.WriteLine("No matches"); } else { Console.WriteLine(maxResult); } break; case "min": int minResult = GetMinElement(tokens[1], nums); if (minResult == -1) { Console.WriteLine("No matches"); } else { Console.WriteLine(minResult); } break; case "first": if(CheckCount(int.Parse(tokens[1]), nums)) { Console.WriteLine("Invalid count"); } else { int[] sequence = GetFirstElements(int.Parse(tokens[1]), tokens[2], nums); PrintResult(sequence); } break; case "last": if (CheckCount(int.Parse(tokens[1]), nums)) { Console.WriteLine("Invalid count"); } else { int[] sequence = GetLastElements(int.Parse(tokens[1]), tokens[2], nums); PrintResult(sequence); } break; } } private static void PrintResult(int[] sequnce) { Console.Write("["); Console.Write(string.Join(", ", sequnce)); Console.Write("]"); Console.WriteLine(); } private static int[] GetLastElements(int count, string typeOfNums, int[] nums) { int[] elements; if (typeOfNums == "even") { elements = nums.Where(x => x % 2 == 0).ToArray(); } else { elements = nums.Where(x => x % 2 != 0).ToArray(); } if (elements.Length < count) { count = elements.Length; } return elements.TakeLast(count).ToArray(); } private static bool CheckCount(int count, int[] nums) { if(nums.Length < count) { return true; } else { return false; } } private static int[] GetFirstElements(int count, string typeOfNums, int[] nums) { int[] elements; if (typeOfNums == "even") { elements = nums.Where(x => x % 2 == 0).ToArray(); } else { elements = nums.Where(x => x % 2 != 0).ToArray(); } if(elements.Length < count) { count = elements.Length; } return elements.Take(count).ToArray(); } private static int GetMinElement(string typeOfNums, int[] nums) { int[] elements; if (typeOfNums == "even") { elements = nums.Where(x => x % 2 == 0).ToArray(); } else { elements = nums.Where(x => x % 2 != 0).ToArray(); } if (elements.Length == 0) { return -1; } else { int min = int.MaxValue; for (int i = elements.Length - 1; i >= 0; i--) { if (min > elements[i]) { min = elements[i]; } } return Array.LastIndexOf(nums, min); } } private static int GetMaxElement(string typeOfNums, int[] nums) { int[] elements; if(typeOfNums == "even") { elements = nums.Where(x => x % 2 == 0).ToArray(); } else { elements = nums.Where(x => x % 2 != 0).ToArray(); } if(elements.Length == 0) { return -1; } else { int max = int.MinValue; for (int i = elements.Length - 1; i >= 0; i--) { if(max < elements[i]) { max = elements[i]; } } return Array.LastIndexOf(nums, max); } } private static void ExchangeArray(int index, int[] nums) { int[] tempArr = new int[nums.Length]; int counter = 0; for (int i = index +1; i < nums.Length; i++) { tempArr[counter++] = nums[i]; } for (int i = 0; i <= index; i++) { tempArr[counter++] = nums[i]; } for (int i = 0; i < tempArr.Length; i++) { nums[i] = tempArr[i]; } } } }