using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "InputX"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("8"); WillReturn.Add("5 15"); WillReturn.Add("40 110"); WillReturn.Add("700 1400"); WillReturn.Add("600 800"); WillReturn.Add("900 1500"); WillReturn.Add("1800 2300"); WillReturn.Add("2100 2500"); WillReturn.Add("2400 3000"); //9 } else if (InputPattern == "Input2") { WillReturn.Add("7"); WillReturn.Add("12 12"); WillReturn.Add("54 54"); WillReturn.Add("128 128"); WillReturn.Add("1310 1310"); WillReturn.Add("1193 1193"); WillReturn.Add("1806 1806"); WillReturn.Add("2876 2876"); //1 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static long[] GetSplitArr(string pStr) { return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray(); } struct RangeInfoDef { internal long RangeMin; internal long RangeMax; } static List mRangeInfoList = new List(); static void Main() { List InputList = GetInputList(); long[] wkArr = { }; Action SplitAct = (pStr) => wkArr = GetSplitArr(pStr); foreach (string EachStr in InputList.Skip(1)) { SplitAct(EachStr); RangeInfoDef WillAdd; WillAdd.RangeMin = wkArr[0]; WillAdd.RangeMax = wkArr[1]; mRangeInfoList.Add(WillAdd); } var IndList = new List(); for (int I = 0; I <= mRangeInfoList.Count - 1; I++) { IndList.Add(I); } long Answer = 0; foreach (int[] EachArr in RubyPatternClass.Permutation(IndList, IndList.Count)) { bool IsOK=true; long RunMax = long.MinValue; foreach (int EachInd in EachArr) { long RangeMin = mRangeInfoList[EachInd].RangeMin; long RangeMax = mRangeInfoList[EachInd].RangeMax; if (RunMax <= RangeMin) { RunMax = RangeMin; continue; } if (RangeMin <= RunMax && RunMax <= RangeMax) { RunMax = RunMax; continue; } IsOK = false; break; } if (IsOK) Answer++; } Console.WriteLine(Answer); } } #region RubyPatternClass // Rubyの場合の数 internal static class RubyPatternClass { // 順列を返す private struct JyoutaiDef_Permutation { internal List SelectedIndList; } internal static IEnumerable Permutation(IEnumerable pEnum, int pR) { if (pR == 0) yield break; Type[] pArr = pEnum.ToArray(); if (pArr.Length < pR) yield break; var Stk = new Stack(); JyoutaiDef_Permutation WillPush; for (int I = pArr.GetUpperBound(0); 0 <= I; I--) { WillPush.SelectedIndList = new List() { I }; Stk.Push(WillPush); } while (Stk.Count > 0) { JyoutaiDef_Permutation Popped = Stk.Pop(); // クリア判定 if (Popped.SelectedIndList.Count == pR) { var WillReturn = new List(); Popped.SelectedIndList.ForEach(X => WillReturn.Add(pArr[X])); yield return WillReturn.ToArray(); continue; } for (int I = pArr.GetUpperBound(0); 0 <= I; I--) { if (Popped.SelectedIndList.Contains(I)) continue; WillPush.SelectedIndList = new List(Popped.SelectedIndList) { I }; Stk.Push(WillPush); } } } } #endregion