saprykin's blog

By saprykin, 3 years ago, translation, In English

Hello, Codeforces!

Really sorry

In this post I would like to talk about solving two types of problems, with almost the same solution. Unfortunately, I do not know how popular this idea is. For the first time I came up with it at the regional stage of 2021/22 in informatics for a certain subtask. And few days ago I noticed the idea in a problem from Google Kick Start and 1637E - Best Pair, so I decided to write this post.

Problem 1

Definition: Given an integer $$$0 \leq K < 10^5$$$ and 2 sorted in ascending order arrays $$$a_1, a_2, \ldots, a_n \ (a_i \leq 10^9)$$$ and $$$b_1, b_2, \ldots, b_n \ (b_i \leq 10^9)$$$. Were written $$$n^2$$$ fractions of the form $$$\frac{a_i}{b_j}, 1 \leq i \leq n, 1 \leq j \leq n$$$. After that, all these fractions were sorted in ascending order. You need to print the value of the $$$k$$$-th ascending fraction.

Idea: It would seem to be generated $$$n^2 \leq 10^{10}$$$ and you cant calculate all this fractions for a reasonable time. However, if you look at the $$$K$$$ constraint it can be understood that we are not interested in all $$$n^2$$$ fractions, but in first $$$K$$$. Then let's maintain the minimum number and move from it to the next.

Solution: For convenience reverse $$$b$$$ array, so that it becomes sorted in descending order and denote by $$$(i, j)$$$ fraction $$$\frac{a_i}{b_j}$$$. Not hard to notice, that $$$(i, j) \leq (i + 1, j)$$$ and $$$(i, j) \leq (i, j + 1)$$$. Hence it is clear that $$$(0, 0)$$$ will be the first element in the sorted array. The statement is that if we considered $$$(i, j)$$$ as a minimal fraction, then two fractions are added to the "candidates" for a new minimum: $$$(i + 1, j)$$$ and $$$(i, j + 1)$$$. This seems intuitive, however, just below is an attempt to prove why this is so. The task was reduced to supporting candidates and taking the best of them.

Code
Proof about 2 candidates
Proof of asymptotics

Problem 2

Definition: 1637E - Best Pair
Idea: Note that different $$$cnt_x$$$ no more $$$\sqrt{n}$$$. Create an array $$$A$$$, which in index $$$i$$$ stores a vector sorted in descending order $$$x$$$ such that $$$cnt_x = i$$$. Let's fix $$$cnt_x$$$ and $$$cnt_y$$$. Since we have fixed one of the multipliers, we need to maximize $$$x+y$$$.
For convenience , we denote $$$a = A[cnt_x]$$$, $$$b = A[cnt_y]$$$. As position denote $$$(i, j)$$$. You need to choose a position such that $$$(a_i, b_j)$$$ is not a bad pair. Using the observations that were made above, we note that:

  • Best answer at the "position" $$$(0, 0)$$$
  • If we are not satisfied $$$(i, j)$$$ we are adding candidates $$$(i + 1, j)$$$ and $$$(i, j + 1)$$$

This is the same task 1, but now we are not doing it a fixed number of times, but for now the best pair is bad. My solution: 146149856

Proof of asymptotics

Problem 3

Definition:

Original statements

Idea: Similarly to the problem 2 we have bad sets and we need to choose the best among the others (in this case, the function of the set is the number of complaints). The initial value is not difficult to determine: For each bit, we look independently, it is more profitable to put 0 either 1. Next we try to change exactly one bit. The logic is the same as in the problem 2, but now not 2 transitions, but $$$P$$$. Time complexity $$$O(PMlog(PM))$$$.

Code

Hope it will help someone.

Good luck and have a high rating!

  • Vote: I like it
  • +82
  • Vote: I do not like it

»
3 years ago, # |
  Vote: I like it +15 Vote: I do not like it

For more on this topic (finding the k maximum/minimum items in a large search space), you can check out USACO guide fracturing search and Algorithms Live about fracturing search