// 指定された値よりも大きい値を持つ最初のインデックスを求めます。 // これは、挿入先のインデックスを意味します。 public static int IndexForInsert(IList <int> a, int v) => BinarySearch.First(i => a[i] > v, 0, a.Count);
// 数当てゲーム // https://dentsu-ho.com/articles/6878 public static int GuessNumber(Func <int, int> reply, int max) => BinarySearch.First(n => reply(n) >= 0, 1, max);
public static double Sqrt(double v, int digits = 9) => BinarySearch.First(x => x * x >= v, Math.Min(1, v), Math.Max(1, v), digits);
public static int IndexOfDescending(IList <int> a, int v) { var r = BinarySearch.First(i => a[i] <= v, 0, a.Count); return(r < a.Count && a[r] == v ? r : ~r); }