K - Airdrop
Basic Information
Contest | The 2018 ICPC Asia Qingdao Regional Contest |
Team AC Ratio | 10/373 (2.7%) |
Tutorial
Let's build a new coordinate system, where the original point is on the position of the airdrop. We now study which players may collide with each other.
- Because all players move at the same time, only the players whose distance to the original point are the same may collide with each other.
- Because all players will first move vertically then horizontally, only the players on the same side of the \(y\)-axis may collide with each other.
- According to the problem statement, players on the \(y\)-axis will never collide.
We now consider when the original point moves gradually to the right, what changes will happen to the number of colliding players. According to the second point stated above, we can consider the players on different sides of the \(y\)-axis separately. We now only consider the players on the left side of the \(y\)-axis.
- If no player falls from the \(y\)-axis to the left, because the distances of all players to the original point will be increased by the same value, the number of colliding players will not change.
- If there are some players falling from the \(y\)-axis to the left, let's look at all players whose distance to the original point equals \(d\).
- There are at most \(2\) players falling from the \(y\)-axis, their \(y\)-coordinates are \(d\) and \(-d\).
- Because all colliding players will be eliminated, there will be at most \(1\) surviving player who is already on the left of \(y\)-axis.
- To produce a surviving player with distance \(d\), the total number of these two types of players must be exactly \(1\).
From the above statements we can see that, we are only interested in the \(x\) values of the players and the \(x\) values that differ by exactly \(1\) from them. Only these \(x\) values will change the number of colliding players. Thus we need to calculate at most \(3n\) times.
Implement the procedure above, we can calculate \(L(x)\) and \(R(x)\), indicating that when the \(x\)-coordinate of the airdrop is \(x\), how many players on its left (or right) will survive. The total number of surviving players is \(L(x) + R(x)\), plus the number of players whose \(x\)-coordinate equals \(x\).
The time completixy is \(\mathcal{O}(n\log n)\), because we need to sort and deduplicate the coordinates. The core algorithm runs in \(\mathcal{O}(n)\).
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|