using System; using System.Collections.Generic; using System.Linq; // https://yukicoder.me/problems/no/326 // yukicoder 326 あみだますたー class Program { static string InputPattern = "InputX"; static IEnumerable GetInputList() { string wkStr; while ((wkStr = Console.ReadLine()) != null) yield return wkStr; } static int[] GetSplitArr(string pStr) { return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => int.Parse(pX)).ToArray(); } static void Main() { string[] InputList = GetInputList().ToArray(); int[] wkArr = { }; Action SplitAct = (pStr) => wkArr = GetSplitArr(pStr); int Ind = 0; int N = 0; int K = 0; var GoalDict = new Dictionary(); var CurrDict = new Dictionary(); int[] AArr = new int[0]; foreach (string EachStr in GetInputList()) { if (Ind == 0) { N = int.Parse(EachStr); for (int I = 1; I <= N; I++) { CurrDict[I] = I; } } if (Ind == 1) { K = int.Parse(EachStr); } if (2 <= Ind && Ind <= K + 2) { SplitAct(EachStr); int L = wkArr[0]; int R = wkArr[1]; int tmp = CurrDict[L]; CurrDict[L] = CurrDict[R]; CurrDict[R] = tmp; } if (Ind < K + 2) { AArr = GetSplitArr(EachStr); } Ind++; } for (int I = 0; I <= AArr.GetUpperBound(0); I++) { GoalDict[AArr[I]] = I + 1; } foreach (string EachStr in InputList.Skip(2).Take((int)K)) { SplitAct(EachStr); int L = wkArr[0]; int R = wkArr[1]; int tmp = CurrDict[L]; CurrDict[L] = CurrDict[R]; CurrDict[R] = tmp; } var AnswerList = new List(); for (int I = 1; I <= N; I++) { int SearchVal = GoalDict[I]; int StaInd = 0; for (int J = I + 1; J <= N; J++) { if (CurrDict[J] == SearchVal) { StaInd = J; break; } } // 既にゴールにいる場合 if (I == StaInd) continue; for (int J = StaInd; I + 1 <= J; J--) { // 三角交換 int tmp = CurrDict[J]; CurrDict[J] = CurrDict[J - 1]; CurrDict[J - 1] = tmp; AnswerList.Add(J - 1); } } Console.WriteLine(AnswerList.Count); foreach (int EachL in AnswerList) { Console.WriteLine("{0} {1}", EachL, EachL + 1); } } }