https://codeforces.me/problemset/problem/1175/C can anyone explain the solution of this problem please? why should we check for the range of consecutive k elements here? solution is here 55178770
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
https://codeforces.me/problemset/problem/1175/C can anyone explain the solution of this problem please? why should we check for the range of consecutive k elements here? solution is here 55178770
Название |
---|
Hello. We are given that the array A is sorted.
If you spend a bit of time, you can understand that Fk(x) denotes the distance of (k+1)th nearest point from x.
Observe that nearest points of x are chronological neighbours of x. For example, if we are looking at the Nth nearest point of X, and we have a list of (N-1) nearest points with us, some on left of x and some on right of x, then the required point will either be to the immediate left of an existing point, or immediate right of an existing point (i.e. no gaps). (Let know if you want a proof)
Therefore, by spending a little time, we can observe that (K+1) nearest points of any point x must correspond to a sliding window of size K+1 in the array.
Once that is established, we know that x can be chosen by us. What is the distance of x from each point in the sliding window? Note that it decreases till it reaches the nearest integer to x, and then starts increasing again. Therefore D_(k+1) (the required (k+1)th distance) must be the max of D1 and D2 where D1 is distance of x from the first element in the window and D2 is distance of x from last element in the window.
Now, we need to minimize this distance. You can observe that there is a tradeoff between these 2 distances D1 and D2. As we move x to the left, D1 decreases and D2 increases. (with D1+D2 =constant at all times). The minimum distance must occur at the middle.
Happy to elaborate on any of the numbered points that is not clear.
can't understand the second point. Can you give the proof?
I will proof this by a simple contradiction argument Assume that at least one of the N nearest neighbours of x does not belong to a continuous window of size N containing x. Let us assume this nearest neighbour is Aj. Without loss of generality, assume Aj<x (same proof works for Aj>x). As the window is not continuous as per our assumption, there must exist some element Ak between Aj and x which is not a nearest neighbour of x. But distance of Ak from x is clearly less than distance of Aj from x. So if Aj is a nearest neighbour, Ak must also be a nearest neighbour. That contradicts our assumption.
if we know that the best point have xl limit on the left and xr limit from the right and by limit I mean the limit of numbers that form the first k + 1 distances.
for example if the array is given (note that the array is given is sorted) 1 3 4 5 6 7 and k = 2 if we choose the point x to be 2 that mean the left limit is 1 and right limit is 4 from 1 to 4 those are the numbers that form the first k+1 distances after sorting as we can see 1 — 2, 3 — 2, 4 — 2 which is 1, 1, 2, and we only care about first k + 1 distances because we want to make k+1 th minimal.
now suppose we know what is xl (left limit ) and what is xr so it's greedy to choose x as (xr + xl ) / 2 now let's try every array[i] as the left limit then we are forced to make array[i + k] the right limit and we take the best x from every array[i] we try to take as the left limit.