public int FindMinArrowShots(int[,] points)
    {
        int len = points.GetLength(0);

        Interval[] intervalsStart = new Interval[len];
        for (int i = 0; i < len; i++)
        {
            intervalsStart[i] = new Interval(points[i, 0], points[i, 1]);
        }
        Interval[] intervalsEnd = (Interval[])intervalsStart.Clone();
        Array.Sort(intervalsStart, (x, y) => x.Start.CompareTo(y.Start));
        Array.Sort(intervalsEnd, (x, y) => x.End.CompareTo(y.End));
        int count = 0;
        int cur   = 0;

        for (int i = 0; i < len; i++)
        {
            if (!intervalsEnd[i].Shot)
            {
                count += 1;
                int end = intervalsEnd[i].End;
                while (cur < len && intervalsStart[cur].Start <= end)
                {
                    intervalsStart[cur].Shot = true;
                    cur += 1;
                }
            }
        }
        return(count);
    }
Beispiel #2
0
        public void op_InequalityTest()
        {
            var I1 = new Interval <double>(3, 5);
            var I2 = I1.Clone();
            var I3 = new Interval <double>(5, 7);
            var I4 = I3.IncludeMin(false);

            Assert.IsFalse(I1 != I2, "Интервал {0} не не равен интервалу {1}", I1, I2);
            Assert.IsFalse(I2 != I1, "Интервал {0} не не равен интервалу {1}", I2, I1);

            Assert.IsTrue(I1 != I3, "Интервал {0} не не равен интервалу {1}", I1, I3);
            Assert.IsTrue(I3 != I1, "Интервал {0} не не равен интервалу {1}", I3, I1);

            Assert.IsTrue(I3 != I4, "Интервал {0} не не равен интервалу {1}", I3, I4);
            Assert.IsTrue(I4 != I3, "Интервал {0} не не равен интервалу {1}", I4, I3);
        }
        /// <summary>
        ///   Add a list of objects to a given interval.
        /// </summary>
        /// <param name = "interval">The interval to store the object in.</param>
        /// <param name = "values">The list of objects to store.</param>
        public void Add(Interval <TMath> interval, IList <TObject> values)
        {
            // Check for intersections with existing intervals.
            IList <IntervalValues> intersecting = FindIntersections(interval);

            if (intersecting.Count > 0)
            {
                // A list used to find remnants after cutting intersections.
                var remnants = new List <Interval <TMath> >
                {
                    (Interval <TMath>)interval.Clone()
                };

                // Split up all intersecting intervals.
                foreach (var intersectingRange in intersecting)
                {
                    Interval <TMath> intersection = intersectingRange.Interval.Intersection(interval);

                    SplitRemoveIntersection(intersectingRange, intersection);

                    // Add intersection with objects of both intervals.
                    var mergedObjects = new List <TObject>(intersectingRange.Values);
                    mergedObjects.AddRange(values);
                    _rangedObjects.Add(new IntervalValues(mergedObjects, intersection));

                    // Remove intersections from remnants.
                    var newRemnants = new List <Interval <TMath> >();
                    foreach (var remnant in remnants)
                    {
                        newRemnants.AddRange(remnant.Subtract(intersection));
                    }
                    remnants = newRemnants;
                }

                // Add remnants of the newly added interval.
                foreach (var remnant in remnants)
                {
                    _rangedObjects.Add(new IntervalValues(values, remnant));
                }
            }
            else
            {
                // No intersections, just add.
                _rangedObjects.Add(new IntervalValues(values, interval));
            }
        }