Drilling
InterviewTime limit1sMemory limit128 MB
Given drilling costs at n positions along a segment, find the minimum worst-case total time to locate the reservoir boundary using adaptive queries.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Binary search, Divide and conquer, Greedy
- Solved
- No attempts yet
Problem
Byteman leads a team searching for crude-oil reservoirs. The team has already drilled two boreholes: they found oil at point and confirmed that there is no oil at point . The reservoir occupies a connected piece of the segment with one end exactly at . Byteman now has to determine how far the reservoir reaches along .
This is not simple, because the drilling speed depends on the location, and the team can drill in only one place at a time. Byteman wants to know in advance how much time he will need, in the worst case, to pinpoint the reservoir's boundary.
Split the segment into equal parts. Place at coordinate and at coordinate ; then the candidate points lie at coordinates . Oil reaches coordinate (point ) but not coordinate (point ), so the boundary is the largest coordinate among that still contains oil (it is if none of them do). Drilling a borehole at point takes units of time and reveals whether oil is present there.
Design a drilling plan that minimizes the worst-case total time needed to identify the reservoir's boundary, and report that time.
Input
The first line contains one integer ().
The second line contains integers (), separated by single spaces.
Output
Print a single integer: the smallest worst-case total drilling time that guarantees Byteman can identify the reservoir's boundary.
Hint
Suppose Byteman drills point first, spending time . If oil is found there, he still has to learn how far the reservoir reaches to the right, which takes two more boreholes costing in the worst case, for a total of .
Starting at point turns out to be better. If point has no oil, checking point alone finishes the job. Otherwise, in the worst case he drills points and and finishes with total time . Hence is optimal for this sample.