Flight Boarding Optimization
Time limit2sMemory limit256 MB
You split the rows into k contiguous zones and order their boarding phases, keeping queue order inside each zone, to minimize total boarding difficulty.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Intervals, Prefix sum
- Solved
- No attempts yet
Problem
Peter runs boarding at the Byteland airport, and his job is to make boarding faster. Planes in Byteland have rows, numbered through starting at the front. Every row has six seats, labeled A to F.
The passengers stand in one queue and board the plane one at a time. If passenger sits in row , that passenger's boarding difficulty is the number of passengers who boarded earlier and sit in rows through . The total boarding difficulty is the sum of the difficulties of all passengers. For example, take ten passengers whose seats in queue order are 6A, 4B, 2E, 5F, 2A, 3F, 1C, 10E, 8B, 5A. Their difficulties are 0, 0, 0, 2, 0, 2, 0, 7, 7, 5 and the total is 23.
To speed boarding up, Peter divides the plane into zones. Every zone is a continuous range of rows. Boarding then runs in phases. In each phase Peter calls one zone, and the passengers seated in that zone board in their original queue order. Peter also decides which zone each phase calls.
Take the queue above and split the plane into two zones, rows 5 to 10 and rows 1 to 4. The first phase seats 6A, 5F, 10E, 8B, 5A and the second phase seats 4B, 2E, 2A, 3F, 1C, in that order. The total boarding difficulty is 6.
Given the queue, find the division into zones that minimizes the total boarding difficulty and report that minimum.
Input
The first line contains three integers , , and (, , , ).
The second line contains integers (), where is the row of the -th passenger in the queue.
At most 6 passengers sit in any single row.
Output
Print the minimum possible total boarding difficulty.