2012-05-20 6 views
-2

У меня есть следующая задача. Существует 24-часовой график. И есть некоторые события с фиксированной длиной. У меня есть новое событие с предопределенной длиной (например, 1 час и 25 минут). Задача состоит в том, чтобы знать. - сколько раз я могу вставить эту задачу в течение дня (эта задача не должна пересекать другие событияРасписание мероприятий

enter image description here

+0

И что вы пробовали? –

+0

Я попробовал тривиальное решение, но теперь я ищу специальные альго. – dizpers

+2

На самом деле нет никаких алгоритмов для рассмотрения, поскольку уже наивный алгоритм выполняет задачу в O (n) :))). Но подождите, может быть одно: жертвовать памятью для производительности, поддерживать подсчеты как массив для каждого свободного периода, а когда меняется ежедневное расписание, обновляйте только те свободные периоды, которые изменились. Однако немногого, чтобы выиграть, если ваш день не имеет миллионов секций, таких как ДНК :))) –

ответ

1

наш вопрос может быть рассмотрен немного упрощенно, так пусть ответ будет упрощенным, тоже (в Ruby):

#! /usr/bin/ruby 
DAY_START = 0 
DAY_END = 24*60 
# now, busy periods as array of size-2 arrays: 
BUSY_PERIODS = [[9*60, 9*60+30], [18*60+30, 20*60]] 
ADDITIONAL_EVENT_LENGTH = 1*60 + 25 # that is, 1 hour and 25 minutes 
# in this simple program, 
# time will be expressed as a number of minutes of the day 
# for more advanced use, try to use "Time" class 

# now let define a function to compute the list of free periods 
# from a given list of busy periods: 
def compute_free_periods(list_of_events) 
    # at the begining of the calculation, our whole day is assumed free: 
    free_periods = Array[ [ DAY_START, DAY_END] ] 
    # now, one by one, let us take away the busy periods: 
    list_of_events.each_with_object free_periods do | busy_period, free_periods | 
    # we use 'each_with_object' version of 'each' enumerator 
    # list of free_periods is passed in as an external object 
    # so that we can gradually take each busy period away from it 

    # let us first note the end time for the last free period 
    # ([-1] refers to the last element of the list of free periods, 
    # subsequent [1] to the second element of the size-2 array) 
    last_free_period_end = free_periods[-1][1] 

    # and now, let us split this last free period into two by the 
    # current busy period (busy periods assumed non-overlapping and 
    # sorted in ascending order) 

    # first, end time of the last free period is set to the beginning 
    # of the busy period that we consider in this iteration: 
    free_periods[-1][1] = busy_period[0] 

    # then, additional free period is appended to the list of 
    # free periods usind << operator, starting when the busy period ends: 
    free_periods << [ busy_period[1], last_free_period_end ] 
    end 
end 

# now, let us use the function we just defined: 
free_periods = compute_free_periods(BUSY_PERIODS) 

# Now, for each free period we will count how many times we can stuff 
# in the desired additional event: 
free_period_capacities = free_periods.map { |free_period| 
    # we are using map function for this, which will return 
    # an array of the same length as free_periods, having 
    # performed prescribed modifications on each element: 

    # first, we calculate how many minutes a free period has 
    period_length = free_period[1] - free_period[0] 
    # then, we will use whole-number division to get the number 
    # of additional events that can fit in: 
    period_length/ADDITIONAL_EVENT_LENGTH 
    # (in Ruby, whole number division is the default behavior of/operator) 
} 

# and finally, we sum up the free period capacities for our new event: 
total_events_possible = free_period_capacities.reduce(:+) 
# (summing is done elegantly by using reduce function with + operator) 

# the last remaining thing we can do to consider program finished is to 
puts total_events_possible # print the result on the screen 

Если вы берете комментарии и сокращать его, то программа становится довольно коротким:

#! /usr/bin/ruby 
DAY_START, DAY_END = 0, 24*60 
BUSY_PERIODS = [[9*60, 9*60+30], [18*60+30, 20*60]] 
ADDITIONAL_EVENT_LENGTH = 1*60 + 25 

def compute_free_periods(list_of_events) 
    free_periods = Array[ [ DAY_START, DAY_END] ] 
    list_of_events.each_with_object free_periods do | busy_period, free_periods | 
    last_free_period_end = free_periods[-1][1] 
    free_periods[-1][1] = busy_period[0] 
    free_periods << [ busy_period[1], last_free_period_end ] end 
end 

free_periods = compute_free_periods(BUSY_PERIODS) 
free_period_capacities = free_periods.map { |free_period| 
    period_length = free_period[1] - free_period[0] 
    period_length/ADDITIONAL_EVENT_LENGTH } 
puts (total_events_possible = free_period_capacities.reduce(:+)) 

Вы видите, что алгоритма не так много. Простое кодирование. Встреча eg. Учебник Ole Foul Zed Learn Ruby The Hard Way и начните , прочитав от упражнения 0 до конца. Или вы можете перейти на другой язык, , такой как Python. У Оле Зеда есть преподавательский талант. Если вы цените опыт , не забудьте его купить как электронную книгу.