3509's blog

By 3509, history, 4 years ago, In English

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. $$$i$$$-th line represents $$$i$$$-th column of the matrix.

Output:

  • Two space-separated integers $$$P$$$ and $$$Q$$$.

Example

Input
Output

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 was 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.

Solution:

Spoiler
  • Vote: I like it
  • +11
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +6 Vote: I do not like it

I don't know the answer, but the fact that the greedy solution doesn't work seems like a manifestation of Simpson's Paradox, which basically says best batting averages in individual years might not yield the best batting average over a period of several years. One can use the wiki's numbers to draw up a counterexample with $$$K=2$$$ and the matrix (I'll write as fractions) $$$[\frac{12}{48},\frac{104}{411},\frac{45}{140}]$$$ which is sorted by ratio, but the selection of 1st and 3rd yields $$$\frac{57}{188}$$$ which is better than $$$\frac{149}{551}$$$

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I see. I did have trouble coming up with a counter-example for the greedy sort solution, that was why i crossed my fingers and sent it in.

    Your input was helpful. Thanks!

»
4 years ago, # |
  Vote: I like it +7 Vote: I do not like it
Why the greedy solution fails
Solution explanation
  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it +9 Vote: I do not like it

    First of all, thanks for replying wholeheartedly!

    Wow, the solution is so out-of-nowhere for me I am just at lost for words. There isn't a clue or hint to enable me to think in such "deliberated" way. I even needed ~1 hours to process your explaination. Should have done more math when I was younger...

    Anyway, since I'm here I would like to add a few things on why the initial Greedy solution failed. Frame the problem like this:

    Imagine each column of the matrix is a 2D vector on a plane, top for $$$x-coordinate$$$ bottom for $$$y-coordinate$$$. Angle of a vector (or the line that contains the vector) is related to its slope, which is $$$y/x$$$ (that's inverse the ratio my greedy sort used). By that, our task is now to choose $$$K$$$ vectors so that the vector sum has the greatest angle. And this convinced me why "Sorted by ratio" didn't work by this example:

    img