Пример #1
0
        public static void Run()
        {
            var data = Console.ReadLine().Split();
            var n    = int.Parse(data[0]);
            var m    = int.Parse(data[1]);

            var lineStarts = new int[n];
            var lineEnds   = new int[n];

            for (int i = 0; i < n; i++)
            {
                data          = Console.ReadLine().Split();
                lineStarts[i] = int.Parse(data[0]);
                lineEnds[i]   = int.Parse(data[1]);
            }

            Sort.QuickSort(lineStarts, 0, n - 1);
            Sort.QuickSort(lineEnds, 0, n - 1);

            var points = Console.ReadLine().Split();

            for (var i = 0; i < points.Length; i++)
            {
                var num = LinesAndPoints.GetIntersectionNumber(lineStarts, lineEnds, int.Parse(points[i]));
                Console.Write($"{num} ");
            }
        }
Пример #2
0
        public static void Test(int n = 50000, int m = 50000, int maxCoordinate = 100000000)
        {
            var rand = new Random();

            var lineStarts = new int[n];
            var lineEnds   = new int[n];

            for (int i = 0; i < n; i++)
            {
                lineStarts[i] = rand.Next(-maxCoordinate, maxCoordinate);
                lineEnds[i]   = rand.Next(-maxCoordinate, maxCoordinate);
            }

            Sort.QuickSort2(lineStarts, 0, n - 1);
            Sort.QuickSort2(lineEnds, 0, n - 1);

            var cache          = new Dictionary <int, int>(m);
            var getFromCache   = 0;
            var algorithmFails = 0;

            var counts = Enumerable
                         .Range(0, m)
                         .Select(x => rand.Next(-maxCoordinate, maxCoordinate))
                         .Select(point =>
            {
                var count      = 0;
                var countNaive = 0;
                if (!cache.ContainsKey(point))
                {
                    count = LinesAndPoints.GetIntersectionNumber(lineStarts, lineEnds, point);
                    //countNaive = LinesAndPoints.GetIntersectionNumberNaive(lineStarts, lineEnds, point);
                    if (count == countNaive || true)
                    {
                        cache.Add(point, count);
                    }
                    else
                    {
                        algorithmFails++;
                        //Console.WriteLine($"Error! {count} != {countNaive}");
                    }
                }
                else
                {
                    count = cache[point];
                    getFromCache++;
                }
                return(count);
            })
                         .ToList();

            //.ForEach(num => Console.Write($"{num} "));

            Console.WriteLine($"{cache.Count} elements in cache. {getFromCache} times get from cache");
            Console.WriteLine($"{counts.Count} points processed");
            Console.WriteLine($"{algorithmFails} times algo failed");
            Console.WriteLine("\n------------------------\n\n");
        }