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:
C++
#include <bits/stdc++.h>using namespace std;using ll = long long;int n;vector<ll> p;ll x, a, y, b, k;
Java
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;import java.util.Comparator;public final class SaveNature {public static void main(String[] args) throws IOException {BufferedReader read = new BufferedReader(new InputStreamReader(System.in));int queryNum = Integer.parseInt(read.readLine());
Python
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!