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

Правка en8, от 3509, 2020-03-06 01:17:39

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

Example

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 + 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
Теги #problem, #help, #binary search

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en24 Английский 3509 2020-03-06 05:54:51 9
en23 Английский 3509 2020-03-06 03:08:19 6
en22 Английский 3509 2020-03-06 01:38:19 0 (published)
en21 Английский 3509 2020-03-06 01:37:47 3
en20 Английский 3509 2020-03-06 01:35:07 52
en19 Английский 3509 2020-03-06 01:32:47 301 Tiny change: '>#include <bits/stdc++.h>\nusing n' -> '>#include \<bits/stdc++.h\>\nusing n'
en18 Английский 3509 2020-03-06 01:24:33 2 Tiny change: '>#include <bits/stdc++.h>\nusing n' -> '>#include \<bits/stdc++.h\>\nusing n'
en17 Английский 3509 2020-03-06 01:23:46 8
en16 Английский 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 Английский 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 Английский 3509 2020-03-06 01:22:05 7
en13 Английский 3509 2020-03-06 01:21:32 85
en12 Английский 3509 2020-03-06 01:19:50 76
en11 Английский 3509 2020-03-06 01:19:04 34
en10 Английский 3509 2020-03-06 01:18:35 24
en9 Английский 3509 2020-03-06 01:18:14 14
en8 Английский 3509 2020-03-06 01:17:39 98
en7 Английский 3509 2020-03-06 01:16:30 12
en6 Английский 3509 2020-03-06 01:16:08 28
en5 Английский 3509 2020-03-06 01:15:38 21
en4 Английский 3509 2020-03-06 01:14:34 181
en3 Английский 3509 2020-03-06 01:10:25 2445 Tiny change: 'ater than , \n' -> 'ater than {10^6} , \n'
en2 Английский 3509 2020-03-06 00:44:09 6 Tiny change: 'Given a 2x$N$ matrix, \' -> 'Given a 2x**N** matrix, \'
en1 Английский 3509 2020-03-06 00:43:53 182 Initial revision (saved to drafts)