Rolling Blackout Grid
HardOnline Assessment
Simulate a regional power grid that sheds loads in priority order when supply drops, and only brings them back after a cooling-off period, over a very long stretch of time.
The grid serves several loads, listed in loads from the most important (index 0) to the least important; loads[i] is the demand of load i. The available supply is a repeating daily pattern: during minute t (counting from 0) the supply is supply[t mod supply.length]. Every load starts on. Each minute, in this order:
- Shed. While the total demand of the loads that are on exceeds that minute's supply, switch off the least important load that is still on (the one with the highest index). It becomes *shed* with a wait counter of 0.
- Restore. Look only at the most important shed load (the lowest index). It is switched back on if its wait counter is at least
holdand the demand of all loads on, plus its own demand, plusmargin, is at most the supply. At most one load is restored per minute, and if the most important shed load cannot be restored, no other shed load is considered. - Count. Every load that is shed at the end of the minute is counted as shed for this minute. Then the wait counter of every shed load increases by 1.
The simulation runs for minutes minutes, which can be as large as 10^12.
Input: supply, a list of non-negative integers; loads, a list of positive demands; margin and hold, non-negative integers; minutes, the number of minutes to simulate.
Output: a list with, for each load, the number of minutes (among minutes 0 to minutes - 1) in which it was counted as shed. With minutes = 0 every count is 0.
Example 1
- Input:
- [[10,10,4,4,10,10],[3,4,2,5],1,2,6]
- Output:
- [0,2,3,6]
- Explanation
- Minute 0 (supply 10, demand 14): the least important load (5) is shed. Minute 1: the shed load has waited only 1 < 2 minutes. Minute 2 (supply 4): loads 2 and 4 are shed as well, leaving only the demand 3. Minute 3: the most important shed load (4) has waited 1. Minute 4 (supply 10): it has waited 2 and 3 + 4 + margin 1 = 8 ≤ 10, so it is restored. Minute 5: the demand 2 load is restored too (7 + 2 + 1 = 10 ≤ 10). The load of 5 never fits (it would need 3 + 4 + 2 + 5 + 1 = 15). Shed minutes: 0, 2, 3, 6.
Example 2
- Input:
- [[11,5,7,12,2],[3,5,2,3],0,1,10]
- Output:
- [2,7,9,10]
- Explanation
- Minute 0 sheds the demand-3 load (supply 11 < 13). Minute 1 (supply 5) sheds the demand-2 and demand-5 loads, leaving 3. Minute 2 (supply 7): the demand-5 load, the most important shed one, has waited enough but 3 + 5 = 8 > 7; the demand-2 load would fit, but it may not jump the queue. Minute 3 (supply 12): the demand-5 load returns. Minute 4 (supply 2) sheds loads 5 and 3, so the top load is shed too, and the pattern continues. The counts after 10 minutes are 2, 7, 9, 10.
Example 3
- Input:
- [[3,9,2],[1,5,5],1,2,1000000000000]
- Output:
- [0,666666666668,1000000000000]
- Explanation
- Minutes 0 to 3: the supply of 3 forces out both big loads, and at minute 4 (supply 9) the demand-5 load that has waited at least 2 minutes is restored (1 + 5 + margin 1 = 7 ≤ 9). The other 5-demand load can never return (1 + 5 + 5 + 1 = 12 > 9). From minute 4 on the grid repeats every 3 minutes: shed at supply 2, shed at supply 3, restored at supply 9 (the wait counter has reached 2). So in 1012 minutes the first big load is shed 666666666668 times and the other one in every minute.
Constraints
- 1 ≤ supply.length ≤ 30; 0 ≤ supply[i] ≤ 100
- 1 ≤ loads.length ≤ 8; 1 ≤ loads[i] ≤ 50
- 0 ≤ margin ≤ 100; 0 ≤ hold ≤ 20
- 0 ≤ minutes ≤ 1012