fork(1) download
  1. ; marzullo's algorithm
  2.  
  3. (define (make-table arrivals departures)
  4. (sort
  5. (append
  6. (map (lambda (x) (cons x -1)) arrivals)
  7. (map (lambda (x) (cons x 1)) departures))
  8. (lambda (xs ys)
  9. (if (= (car xs) (car ys))
  10. (< (cdr xs) (cdr ys))
  11. (< (car xs) (car ys))))))
  12.  
  13. (define (marzullo arrivals departures)
  14. (let ((best 0) (cnt 0) (beststart 0) (bestend 0))
  15. (let loop ((t (make-table arrivals departures)))
  16. (if (null? t)
  17. (values best beststart bestend)
  18. (begin
  19. (set! cnt (- cnt (cdar t)))
  20. (when (< best cnt)
  21. (set! best cnt)
  22. (set! beststart (caar t))
  23. (set! bestend (caadr t)))
  24. (loop (cdr t)))))))
  25.  
  26. (define arrivals '(10 12 11 13 14 12 9 14 12 10))
  27. (define departures '(15 14 17 15 15 16 13 15 17 18))
  28.  
  29. (call-with-values
  30. (lambda () (marzullo arrivals departures))
  31. (lambda (best beststart bestend)
  32. (display best) (newline)
  33. (display beststart) (newline)
  34. (display bestend) (newline)))
  35.  
  36. (define departures '(15 15 17 15 15 16 15 15 17 18))
  37.  
  38. (call-with-values
  39. (lambda () (marzullo arrivals departures))
  40. (lambda (best beststart bestend)
  41. (display best) (newline)
  42. (display beststart) (newline)
  43. (display bestend) (newline)))
Success #stdin #stdout 0.02s 8672KB
stdin
Standard input is empty
stdout
9
14
14
10
14
15