Блог пользователя nilesh8757

Автор nilesh8757, история, 7 лет назад, По-английски

Can anyone share their approach for problem E and F from the contest ?

  • Проголосовать: нравится
  • +12
  • Проголосовать: не нравится

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +16 Проголосовать: не нравится

For E the problem can be reduced to finding sum of Manhattan distances between every pair of points . Now it can be seen that in all possible combination of selected k squares every pair of points will occur C(n*m-2,k-2) times as we have to select k-2 points from n*m-2 points as we have already fixed 2 points. So the answer can be simply written as = C(n*m-2,k-2)*d where d denotes the sum of Manhattan distances between all pair of points which can be easily found out

»
7 лет назад, скрыть # |
 
Проголосовать: нравится +14 Проголосовать: не нравится

F: First, notice that the b values don't matter, accumulate them in a separate variable and add them to the answer when outputting.

Finding the $$$x$$$ that minimizes $$$\sum |x - a_i|$$$ is equivalent to finding the median of the set of $$${ a_i }$$$. This is a classic problem, which can be solved with two sets (or multisets if the values can repeat). Actually computing the function also requires storing the sum of each set.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone explain F Binary indexed tree solution, plz

»
7 лет назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

The solutions have already been explained by other people here. :)

The main insight to solve E is to count the number of times each pair is counted, rather than iterating over all $$$K$$$ combinations.

The main insight to solve F is to notice that Bs are constant, and that the answer to minimise $$$f(x)$$$ is the median. We can evaluate the function $$$f(x)$$$ quickly by dividing all the A's into two halves $$$A_1$$$ and $$$A_2$$$. Let $$$A_1$$$ contain all elements smaller than or equal to the median and let $$$A_2$$$ contain all elements larger than the median. If we just know the sum of $$$A_1$$$ and $$$A_2$$$, then we can find out $$$f(x)$$$ in $$$O(1)$$$ time.

Here is my GitHub repository consisting of all my programs and explanations. As an experiment, I have started posting some 'rough' notes that I make while working my way through the problem as well. It is not intended to be the full and formal solution. Just some thoughts and key points.