Problem:
Given a $$$2*N$$$ matrix contains of positive integers with value not greater than $$$10^6$$$. Given an integer $$$K$$$.
Chooses $$$K$$$ column(s) from the matrix, call $$$P$$$ is the sum of all of the top integers from chosen columns, call $$$Q$$$ is the sum of all of the bottom integers from the chosen columns.
Your task is to maximizes $$$P/Q$$$ and print $$$P$$$ and $$$Q$$$ out in its reduced fraction form.
Constraint: $$$1 \leq K \leq N \leq 5*10^4$$$
Time limit: 1s
Input:
First line contains: $$$N$$$, $$$K$$$
Next $$$N$$$ lines contain two integers, first integer belongs in the top row of the matrix, second integer belongs in the bottom row.
Output:
- Two space-separated integers $$$P$$$ and $$$Q$$$.
Example
Attempts:
It was from our school's training contest that I first saw it. I blindly submitted a greedy sort sol (ratio of $$$top/bot$$$) but got WA. Next I tried to make it TLE, $$$K$$$ iterations with everytime choose a col. that maximizes $$$(currentP + top)/(currentQ + bot)$$$ and it did get a TLE.
But after that I were stumped.
I aksed my coach for the solution code and let the explaination be an excercise but I couldn't figure it out.
Please help me understand the math/intuition behind the idea of the solution.
I apologize that I can't provide submit link.