Segment Pricing
InterviewTime limit1sMemory limit128 MB
Choose a non-increasing fare for each boarding stop, with riders boarding only if their budget covers it, to maximize total revenue.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Sorting, Array
- Solved
- No attempts yet
Problem
You run a subway line and want to set fares that maximize revenue, assuming you know exactly who will ride and each rider's budget.
The line has stops and therefore segments between consecutive stops. For simplicity, every rider is heading to the final stop , but riders board at different stops. You must choose a fare for each boarding stop (for ).
For fairness, a stop farther from the destination may not be cheaper than one closer to it: the fare at stop must be at least the fare at stop , since it covers a longer distance.
For each stop you are given the exact budget of every rider who wants to board there. A rider boards only if their budget is at least that stop's fare; otherwise they walk and pay nothing. No fare may exceed $5.00 (that is, cents), and every fare is between and cents. The subway always has enough seats for everyone.
Maximize the total revenue, where the revenue collected at a stop is its fare multiplied by the number of riders who board there.
Input
The first line contains the number of data sets . Each data set has the following form:
- The first line contains an integer (), the number of stops.
- The next lines describe the boarding riders; line lists the riders boarding at stop (all of whom get off at stop ). That line contains integers (): the budgets (in cents) of those riders, given in non-decreasing order. An empty line means no rider boards at that stop.
A budget may exceed ; such a rider still boards at any fare up to the -cent cap.
Output
For each data set, print Data Set x: on its own line, where is the data set's number (starting at ), then print the maximum revenue in cents on the next line. Separate consecutive data sets with a single blank line.