Engineering platform~25 min2s · 256 MBtarget O(q log T) time, O(q) spacebinary-search-answer
A build platform runs test jobs on two agents. Agent A starts working after delay_a minutes of set-up, and then completes rate_a jobs every minute. Agent B does the same with delay_b and rate_b.
Work is continuous: after half a minute an agent has completed half of its per-minute rate. Before its set-up ends, an agent completes nothing. Both agents work on the same pile of jobs. A team asks how long a batch will take.
Write finish_times(delay_a, rate_a, delay_b, rate_b, orders).
orders[j] is the number of jobs in batch j. Each batch starts at minute 0 with both agents fresh.orders[j] jobs.orders.Input: delay_a = 1, rate_a = 2, delay_b = 3, rate_b = 3, orders = [5]
Output: [320]After t hundredths of a minute, agent A has completed 2 × max(0, t - 100) hundredths of a job, and agent B has completed 3 × max(0, t - 300). The batch needs 5 × 100 = 500. The search covers the times from 0 to 800, where 800 is the later start, 300, plus 500.
| lo | hi | t tested | Work done | Enough? | Decision |
|---|---|---|---|---|---|
| 0 | 800 | 400 | 900 | yes | hi becomes 400 |
| 0 | 400 | 200 | 200 | no | lo becomes 201 |
| 201 | 400 | 300 | 400 | no | lo becomes 301 |
| 301 | 400 | 350 | 650 | yes | hi becomes 350 |
| 301 | 350 | 325 | 525 | yes | hi becomes 325 |
| 301 | 325 | 313 | 465 | no | lo becomes 314 |
| 314 | 325 | 319 | 495 | no | lo becomes 320 |
| 320 | 325 | 322 | 510 | yes | hi becomes 322 |
| 320 | 322 | 321 | 505 | yes | hi becomes 321 |
| 320 | 321 | 320 | 500 | yes | hi becomes 320 |
The window closes at 320, which is 3.20 minutes. At 319 the work is 495, which is not enough.
Input: delay_a = 0, rate_a = 3, delay_b = 0, rate_b = 4, orders = [1]
Output: [15]Together the agents complete 7 jobs a minute, so one job takes 0.1428... minutes. Rounded up to hundredths, that is 0.15 minutes, or 15.
Input: delay_a = 0, rate_a = 10, delay_b = 100, rate_b = 1, orders = [5]
Output: [50]Agent B is still in set-up for the whole batch. Agent A finishes 5 jobs in half a minute.
0 ≤ delay_a, delay_b ≤ 1,000,000 (minutes of set-up)1 ≤ rate_a, rate_b ≤ 1,000 (jobs a minute)1 ≤ orders.length ≤ 100,0001 ≤ orders[j] ≤ 1,000,000,000 (jobs in a batch)Stepping through the times one hundredth at a time takes about 5 × 10^10 steps for the largest batch. That is far over the budget of about 10^8 a second. The work done never goes down as time passes, so each batch can be answered by halving the range of times.