Beispiel #1
0
        static void Main(string[] args)
        {
            // read for m and k

            var MAndK = Console.ReadLine();
            //var MAndK = "5 120";

            var MKArray = MAndK.Split(' ');

            var numberOfSlots = int.Parse(MKArray[0]);
            var minutes = int.Parse(MKArray[1]);

            var slots = new Interval[numberOfSlots];

            for (int i = 0; i < numberOfSlots; i++)
            {
                var slotInput = Console.ReadLine();
                var slotArray = slotInput.Split(' ');
                var timeSlot = new Interval();
                timeSlot.Start =int.Parse(slotArray[0].Trim())*60 + int.Parse(slotArray[1].Trim());
                timeSlot.End = int.Parse(slotArray[2].Trim())* 60 + int.Parse(slotArray[3].Trim());

                slots[i] = timeSlot;
            }

            // merge intervals
            var mergedSlots = Merge(slots);

            //scan for free slot that has value bigger than minutes
            var freeSlots = new List<Interval>();

            if (mergedSlots[0].Start> minutes)
            {
                // add the first free slot
                freeSlots.Add(new Interval
                {
                    Start = 0,
                    End = mergedSlots[0].Start
                });
            }

            for (int i = 1; i < mergedSlots.Count() ; i++)
            {
                if ((mergedSlots[i].Start - mergedSlots[i-1].End>=minutes))
                {
                    freeSlots.Add(new Interval
                    {
                        Start = mergedSlots[i - 1].End,
                        End = mergedSlots[i].Start
                    });
                }

            }
            // check the last

            if ((24*60 - mergedSlots.Last().End) > minutes)
            {
                freeSlots.Add(new Interval
                {
                    Start = mergedSlots.Last().End,
                    End = 0
                });
            }

            // print out free slots
            foreach (var freeslot in freeSlots)
            {

                Console.WriteLine("{0} {1} {2} {3}"
                    , (100 + freeslot.Start/60).ToString().Substring(1, 2)
                    , (100 + freeslot.Start%60).ToString().Substring(1, 2)
                    , (100 + freeslot.End/60).ToString().Substring(1, 2)
                    , (100 + freeslot.End % 60).ToString().Substring(1, 2)

                    );
            }
        }
Beispiel #2
0
        private static List<Interval> Merge(Interval[] intervals)
        {
            List<Interval> merged = new List<Interval>();

            var sorted =  intervals.OrderBy(interval => interval.Start).ToArray();

            for (int i=0; i< sorted.Length-1; i++)
            {
                if (sorted[i].End >= sorted[i + 1].Start)
                {
                    // merge to make i +1 item the merged one
                    sorted[i + 1].Start = sorted[i].Start;
                    // need to check end too
                    if (sorted[i].End > sorted[i + 1].End)
                    {
                        sorted[i + 1].End = sorted[i].End;
                    }
                }
                else
                {
                    merged.Add(sorted[i]);
                }

            }
            // add last
            merged.Add(sorted[sorted.Length - 1]);

            return merged;
        }