Hello, guys!
I need help in this problem 491B - New York Hotel, there is no tutorials for this round and I can not get the idea to solve it.
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
Hello, guys!
I need help in this problem 491B - New York Hotel, there is no tutorials for this round and I can not get the idea to solve it.
Название |
---|
Imagine that, instead of writing a point as
(X, Y)
, we think of it as(X' = X + Y, Y' = X - Y)
. Visually, you can think of this transformation as rotating the grid by 45 degrees, so that the diagonals become the rows and columns.Before the transformation, the distance between two points was given by
abs(X1 - X2) + abs(Y1 - Y2)
, which ismax(X1 - X2, X2 - X1) + max(Y1 - Y2, Y2 - Y1)
. It's hard to simplify things when you have operators like sum and max mixed together. We cannot do anything simple like looking only at the furthest point in each direction. At the same time, considering all of the points is too expensive.But now, distance is given by
max(max(X'1 - X'2, X'2 - X'1), max(Y'1 - Y'2, Y'2 - Y'1))
(can you see why?). In order to determine how good a restaurant is, we're interested in the maximum over all of the distances to the hotels. Since we are dealing only withmax
, we are free to regroup the terms as we want. So, let's find the maximum possible(R'X - H'X)
, the maximum possible(H'X - R'X)
, etc. To do that, we only need to consider the most extreme values ofH'X
andH'Y
among the hotels, and that's easy to do.You can see my implementation here: 8840674.
Why the distance given by
max(max(X'1 - X'2, X'2 - X'1), max(Y'1 - Y'2, Y'2 - Y'1))
after this transformation?! I can not get this part!!consider you want to calculate distance between two points B and A. there are 4 cases : 1) point B is at right down of A then the distance will be :
xA+yA-(xB+yB)
2) point B is at left up of A then the distance will be :xB+yB-(xA+yA)
3) point B is at left down of A then the distance will be :xA-yA-(xB-yB)
4) point B is at right up of A then the distance will be :xB-yB-(xA-yA)
so the maximum distance will be :max(max(X'1 - X'2, X'2 - X'1), max(Y'1 - Y'2, Y'2 - Y'1))
One way to see it is that the four moves you can make in the grid, which were previously
(0, 1), (1, 0), (0, -1), and (-1, 0)
, look like(1, -1), (1, 1), (-1, 1), (-1, -1)
along the rotated axes.That means two things. One, we can handle the two dimensions
X' and Y'
independently. Two, if you have a differenceDX' = X'1 - X'2
, you needDX'
moves to cover the difference, and then you are allowed to make an even number of extra steps (we can throw them away by alternating +1 and -1).DX'
andDY'
always have the same parity, so we can just pick the bigger one as our number of steps, and it is clearly minimal.