Please read the new rule regarding the restriction on the use of AI tools. ×

Need help w/problem! Selects K different column(s) from a 2xN matrix, maximizes the ratio of (sum of selected top) to (sum of selected bottom).

Revision en3, by 3509, 2020-03-06 01:10:25

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 <= K <= N <= 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$$$.

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 + bottm)$$$ 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 It seemed to be beyond me.

Please help me understand the math/intuition behind the idea of the solution. I apologize that I can't provide submit link.

Solution:

Spoiler
Tags #problem, #help, #binary search

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en24 English 3509 2020-03-06 05:54:51 9
en23 English 3509 2020-03-06 03:08:19 6
en22 English 3509 2020-03-06 01:38:19 0 (published)
en21 English 3509 2020-03-06 01:37:47 3
en20 English 3509 2020-03-06 01:35:07 52
en19 English 3509 2020-03-06 01:32:47 301 Tiny change: '>#include <bits/stdc++.h>\nusing n' -> '>#include \<bits/stdc++.h\>\nusing n'
en18 English 3509 2020-03-06 01:24:33 2 Tiny change: '>#include <bits/stdc++.h>\nusing n' -> '>#include \<bits/stdc++.h\>\nusing n'
en17 English 3509 2020-03-06 01:23:46 8
en16 English 3509 2020-03-06 01:22:55 6 Tiny change: 'raint:_ $1 <= K <= N <= 5*10^4$\n\' -> 'raint:_ $1\leqK\leqN\leq5*10^4$\n\'
en15 English 3509 2020-03-06 01:22:37 24 Tiny change: 'raint:_ $1 <= K <= N <= 5*10^4$\n\' -> 'raint:_ $1\leqK\leqN\leq5*10^4$\n\'
en14 English 3509 2020-03-06 01:22:05 7
en13 English 3509 2020-03-06 01:21:32 85
en12 English 3509 2020-03-06 01:19:50 76
en11 English 3509 2020-03-06 01:19:04 34
en10 English 3509 2020-03-06 01:18:35 24
en9 English 3509 2020-03-06 01:18:14 14
en8 English 3509 2020-03-06 01:17:39 98
en7 English 3509 2020-03-06 01:16:30 12
en6 English 3509 2020-03-06 01:16:08 28
en5 English 3509 2020-03-06 01:15:38 21
en4 English 3509 2020-03-06 01:14:34 181
en3 English 3509 2020-03-06 01:10:25 2445 Tiny change: 'ater than , \n' -> 'ater than {10^6} , \n'
en2 English 3509 2020-03-06 00:44:09 6 Tiny change: 'Given a 2x$N$ matrix, \' -> 'Given a 2x**N** matrix, \'
en1 English 3509 2020-03-06 00:43:53 182 Initial revision (saved to drafts)