fork download
  1. Problem 2: Bookshelf [Neal Wu / Traditional, 2012]
  2.  
  3. When Farmer John isn't milking cows, stacking haybales, lining up his cows,
  4. or building fences, he enjoys sitting down with a good book. Over the
  5. years, he has collected N books (1 <= N <= 100,000), and he wants to build
  6. a new set of bookshelves to hold them all.
  7.  
  8. Each book i has a width W(i) and height H(i). The books need to be added
  9. to a set of shelves in order; for example, the first shelf should contain
  10. books 1...k for some k, the second shelf should start with book k+1, and so
  11. on. Each shelf can have a total width of at most L (1 <= L <=
  12. 1,000,000,000). The height of a shelf is equal to the height of the
  13. tallest book on that shelf, and the height of the entire set of bookshelves
  14. is the sum of the heights of all the individual shelves, since they are all
  15. stacked vertically.
  16.  
  17. Please help FJ compute the minimum possible height for the entire set of
  18. bookshelves.
  19.  
  20. PROBLEM NAME: bookshelf
  21.  
  22. INPUT FORMAT:
  23.  
  24. * Line 1: Two space-separated integers: N and L.
  25.  
  26. * Lines 2..1+N: Line i+1 contains two space-separated integers: H(i)
  27. and W(i). (1 <= H(i) <= 1,000,000; 1 <= W(i) <= L).
  28.  
  29. SAMPLE INPUT (file bookshelf.in):
  30.  
  31. 5 10
  32. 5 7
  33. 9 2
  34. 8 5
  35. 13 2
  36. 3 8
  37.  
  38. INPUT DETAILS:
  39.  
  40. There are 5 books. Each shelf can have total width at most 10.
  41.  
  42. OUTPUT FORMAT:
  43.  
  44. * Line 1: The minimum possible total height for the set of
  45. bookshelves.
  46.  
  47. SAMPLE OUTPUT (file bookshelf.out):
  48.  
  49. 21
  50.  
  51. OUTPUT DETAILS:
  52.  
  53. There are 3 shelves, the first containing just book 1 (height 5, width 7),
  54. the second containing books 2..4 (height 13, width 9), and the third
  55. containing book 5 (height 3, width 8).
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty