static long Solve(long[] s, long[] t, long x) { long ans = 1000000000000; long sl, sr, tl, tr; int i = Bisect.BisectRight(s, x); sl = s[i - 1]; sr = s[i]; int j = Bisect.BisectRight(t, x); tl = t[j - 1]; tr = t[j]; ans = Math.Max(sr, tr) - x; long d = x - Math.Min(sl, tl); ans = Math.Min(ans, d); long d_sr = sr - x; long d_tl = x - tl; long d1 = 2 * Math.Min(d_sr, d_tl) + Math.Max(d_sr, d_tl); ans = Math.Min(ans, d1); long d_sl = x - sl; long d_tr = tr - x; long d2 = 2 * Math.Min(d_tr, d_sl) + Math.Max(d_tr, d_sl); ans = Math.Min(ans, d2); return(ans); }
public void BisectsCorrectly() { ReadOnlySpan <int> values = new int[] { 1, 3, 3, 5, 5, 5, 7, 7, 9 }; Assert.Equal(0, Bisect.Left(values, 0)); Assert.Equal(0, Bisect.Right(values, 0)); Assert.Equal(3, Bisect.Left(values, 5)); Assert.Equal(6, Bisect.Right(values, 5)); }
public static void Main() { int[] a = { 1, 2, 3, 3, 4, 4, 5 }; AssertEquals(4, Bisect.BisectRight(a, 3)); AssertEquals(2, Bisect.BisectLeft(a, 3)); double[] b = { 1.0, 2.0, 3.14, 4.28, 5.01 }; AssertEquals(2, Bisect.BisectRight(b, 3.0)); AssertEquals(2, Bisect.BisectLeft(b, 3.0)); }
public void TestReverse() { var haystack = new int[] { 100, 100, 99, 98, 97 }; var needle = new int[] { 100, 99, 98, 97 }; var leftExpected = new int[] { 0, 2, 3, 4 }; var rightExpected = new int[] { 2, 3, 4, 5 }; var reverseComparer = Comparer <int> .Create((x, y) => y - x); var bisect = new Bisect <int>(haystack, reverseComparer); Assert.Equal(rightExpected, needle.Select(bisect.Right).ToArray()); Assert.Equal(leftExpected, needle.Select(bisect.Left).ToArray()); }
public void TestBisect() { var haystack = new int[] { 1, 4, 5, 6, 8, 12, 15, 20, 21, 23, 23, 26, 29, 30 }; var needle = new int[] { 0, 1, 2, 5, 8, 10, 22, 23, 29, 30, 31 }; var rightExpected = new int[] { 0, 1, 1, 3, 5, 5, 9, 11, 13, 14, 14 }; var leftExpected = new int[] { 0, 0, 1, 2, 4, 5, 9, 9, 12, 13, 14 }; // items provided to the Bisect object is sorted order // from there, it will find item left of 8 var bisect = new Bisect <int>(haystack, Comparer <int> .Default); Assert.Equal(rightExpected, needle.Select(bisect.Right).ToArray()); Assert.Equal(leftExpected, needle.Select(bisect.Left).ToArray()); }
private static void test03(int nbits, double xmin, double xmax, int n) //****************************************************************************80 // // Purpose: // // TEST03 tests WAPR(X) when X is the argument. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 17 June 2014 // // Author: // // Original FORTRAN77 version by Andrew Barry, S. J. Barry, // Patricia Culligan-Hensley. // C++ version by John Burkardt. // // Parameters: // // Input, int NBITS, the number of bits in the mantissa. // // Input, double XMIN, XMAX, the range. // // Input, int N, the number of equally spaced values // in the range at which arguments are to be chosen. // { int i; int iw; int nd; int ner = 0; int nerror = 0; double w; double we; double x; Bisect.BisectData bdata = new(); WAPRData wdata = new(); Console.WriteLine(""); Console.WriteLine("TEST03"); Console.WriteLine(" Input X is the argument."); const int l = 0; const int ifmt = 1; if (xmax < xmin) { (xmin, xmax) = (xmax, xmin); } double dx = (xmax - xmin) / n; xmin -= dx; switch (xmax) { case <= 0.0: iw = 1; Console.WriteLine(" Both branches of the W function will be checked."); break; default: iw = 0; Console.WriteLine(" Wp has been selected (maximum x is > 0)"); break; } Console.WriteLine(""); Console.WriteLine(" Results for Wp(x):"); switch (ifmt) { case 0: Console.WriteLine(""); Console.WriteLine(" Offset x W(x) (WAPR)" + " W(x) (BISECT) Digits Correct"); break; default: Console.WriteLine(""); Console.WriteLine(" x W(x) (WAPR)" + " W(x) (BISECT) Digits Correct"); break; } Console.WriteLine(""); for (i = 1; i <= n + 1; i++) { x = xmin + i * dx; w = WAPR.wapr(ref wdata, x, 0, ref nerror, l); switch (nerror) { case 1: Console.WriteLine(" The value of X = " + x + " is out of range."); break; default: { we = Bisect.bisect(ref bdata, x, 0, ref ner, l); switch (ner) { case 1: Console.WriteLine(""); Console.WriteLine(" BISECT did not converge for x = " + x + ""); Console.WriteLine(" Try reducing NBITS."); break; } if (Math.Abs(w - we) <= double.Epsilon) { nd = (int)(Math.Log10(Math.Pow(2.0, nbits)) + 0.5); } else { nd = (int)(Math.Log10(Math.Abs(we / (w - we))) + 0.5); } Console.WriteLine(x.ToString("0.########").PadLeft(17) + w.ToString("0.########").PadLeft(17) + we.ToString("0.########").PadLeft(17) + " " + nd.ToString(CultureInfo.InvariantCulture).PadLeft(3) + ""); break; } } } switch (iw) { case 1: { Console.WriteLine(""); Console.WriteLine(" Results for Wm(x):"); switch (ifmt) { case 0: Console.WriteLine(""); Console.WriteLine(" Offset x W(x) (WAPR)" + " W(x) (BISECT) Digits Correct"); break; default: Console.WriteLine(""); Console.WriteLine(" x W(x) (WAPR)" + " W(x) (BISECT) Digits Correct"); break; } for (i = 1; i <= n + 1; i++) { x = xmin + i * dx; w = WAPR.wapr(ref wdata, x, 1, ref nerror, l); switch (nerror) { case 1: Console.WriteLine(" The value of X = " + x + " is out of range."); break; default: { we = Bisect.bisect(ref bdata, x, 1, ref ner, l); switch (ner) { case 1: Console.WriteLine(""); Console.WriteLine(" BISECT did not converge for x = " + x + ""); Console.WriteLine(" Try reducing NBITS."); break; } if (Math.Abs(w - we) <= double.Epsilon) { nd = (int)(Math.Log10(Math.Pow(2.0, nbits)) + 0.5); } else { nd = (int)(Math.Log10(Math.Abs(we / (w - we))) + 0.5); } Console.WriteLine(x.ToString("0.########").PadLeft(17) + w.ToString("0.########").PadLeft(17) + we.ToString("0.########").PadLeft(17) + " " + nd.ToString(CultureInfo.InvariantCulture).PadLeft(3) + ""); break; } } } break; } } }