Explanation
Binary search on the answer for the minimum number of tickets, , to sell to make the total ecological contribution to at least .
Let's try to determine the maximum contribution attainable with tickets.
The problem indicates that only of the -th ticket and of the -th ticket could contribute to the total ecological contribution. We can precompute these multipliers (either , , , or depending on the ticket) over tickets, then greedily match the largest multiplier with the highest value ticket. This can be done by sorting the multipliers and tickets in decreasing order; then the maximum total ecological contribution would be .
Implementation
Time Complexity:
from math import gcdfor _ in range(int(input())):ticket_num = int(input())tickets = sorted([int(i) for i in input().split()], reverse=True)prog1 = [int(i) for i in input().split()]prog2 = [int(i) for i in input().split()]min_revenue = int(input())# the frequency of both programs including a single ticketcombo_freq = prog1[1] * prog2[1] // gcd(prog1[1], prog2[1])
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!