Problem 362
Minimum Days to Make Bouquets
Medium · 20 pts · Array, Binary Search
Problem
A garden has N flowers in a row, and flower i blooms on day bloom[i]. To make one bouquet you need K adjacent flowers that have all bloomed, and each flower can be used in at most one bouquet. Find the minimum number of days you must wait to make M bouquets, or print -1 if it is impossible even after every flower has bloomed. If you can make M bouquets on day D, you can also make them on any later day, so binary search on D works.
Input
First line: N, M and K. Second line: the N bloom days.
Output
Print the minimum number of days, or -1.
Constraints
1 <= N <= 100000; 1 <= M <= 1000000; 1 <= K <= N; 1 <= bloom[i] <= 1000000000
Example input
5 3 1 1 10 3 10 2
Example output
3
Explanation
With bloom days 1 10 3 10 2, M = 3 and K = 1: by day 3 the flowers blooming on days 1, 3 and 2 are ready, enough for three single-flower bouquets. On day 2 only two are ready, so the answer is 3.