How many extra tickets can a flight sell?
A regional airline in Johannesburg sells more tickets than seats because some passengers never show up. Sell too many and people are bumped (a refund plus compensation); sell too few and seats fly empty. The analysts run a Monte-Carlo experiment, but it must be fully reproducible, so the random numbers come from a fixed linear congruential generator (LCG).
Task: line 1 is seats p trials seed (p is the show-up percentage of each ticket holder). Line 2 is fare comp max_extra (integers). The LCG is x = (1103515245 * x + 12345) mod 2^31, starting with x = seed; every draw first updates x, then a passenger shows up if (x // 65536) % 100 < p. For every extra from 0 to max_extra: restart the generator from seed, sell tickets = seats + extra tickets, and simulate trials flights one after another (each flight draws one number per ticket, in order, continuing the same stream). Per flight, shows is the number who appear, bumped = max(0, shows - seats) and profit = (tickets - bumped) * fare - bumped * comp. Print one line per extra: extra=E avg_profit=A bump_prob=B, where A is the average profit per flight (2 decimals) and B the fraction of flights with at least one bumped passenger (3 decimals). Finish with Best extra: E, the extra with the highest total profit (the smallest extra wins a tie).
Sample tests
Input
40 90 300 12345 200 400 6
Expected output
extra=0 avg_profit=8000.00 bump_prob=0.000 extra=1 avg_profit=8192.00 bump_prob=0.013 extra=2 avg_profit=8354.00 bump_prob=0.060 extra=3 avg_profit=8458.00 bump_prob=0.163 extra=4 avg_profit=8480.00 bump_prob=0.323 extra=5 avg_profit=8382.00 bump_prob=0.507 extra=6 avg_profit=8210.00 bump_prob=0.673 Best extra: 4
Input
10 80 100 7 150 300 3
Expected output
extra=0 avg_profit=1500.00 bump_prob=0.000 extra=1 avg_profit=1605.00 bump_prob=0.100 extra=2 avg_profit=1660.50 bump_prob=0.250 extra=3 avg_profit=1657.50 bump_prob=0.420 Best extra: 2
Your answer is also checked against 4 hidden tests.
Need a hint?
The generator state must be reset for every value of extra, but NOT between the flights of the same extra.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
