Grocery Shopping for Ingredients
Limits: 1 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Zenyk loves sandwiches very much, and his favorite sandwich consists of bread, sausage, and cheese. One morning, Zenyk noticed that he ran out of sandwich ingredients, so he needs to buy bread, sausage, and cheese at the store. Fortunately, Zenyk lives in a neighborhood with many different stores:
\(n\) bakeries, with the \(i\)-th bakery located at coordinate \(x_i\).
\(n\) cheese shops, with the \(i\)-th cheese shop located at coordinate \(y_i\).
\(n\) meat shops, with the \(i\)-th meat shop located at coordinate \(c_i\).
\(n\) supermarkets, with the \(i\)-th supermarket located at coordinate \(d_i\).
A supermarket has sausage, bread, and cheese all together. However, a supermarket might be located very far away, and then it is easier to buy all the ingredients separately in different stores. Zenyk’s house is located at coordinate 0.
Help Zenyk find the minimum distance he must walk to buy all the necessary ingredients and return back home, since Zenyk is very tired after writing a programming Olympiad.
Input
The first line contains a single integer \(n\).
The second line contains \(n\) integers \(x_i\) — the coordinates of the bakeries.
The third line contains \(n\) integers \(y_i\) — the coordinates of the cheese shops.
The fourth line contains \(n\) integers \(c_i\) — the coordinates of the meat shops.
The fifth line contains \(n\) integers \(d_i\) — the coordinates of the supermarkets.
Output
In a single line, output a single integer — the minimum distance Zenyk needs to walk.
Constraints
\(1 \le n \le 10^5\),
\(-10^9 \le x_i, y_i, c_i, d_i \le 10^9\),
\(5\) points — a block of tests in which \(n = 1\),
\(10\) points — a block of tests in which \(n \le 100\),
\(10\) points — a block of tests in which \(n \le 10^5\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 1 2 3 -1 -10 | 8 |
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Grocery Shopping for Ingredients - Editorial
Clearly, Zenyk should either go to a supermarket or visit a shop of each type separately, since once Zenyk has visited a supermarket, he does not need to go to any other shop. So the problem reduces to two cases: Zenyk goes to a supermarket, or Zenyk buys the products in separate shops.
Consider the case when Zenyk goes to a supermarket. Since we want to minimize the distance to coordinate 0, we should choose the supermarket closest to this coordinate, that is, the supermarket with the smallest \(|d_i|\). This can be done in \(O(n)\).
Now consider the case when Zenyk buys all products in different shops. How do we compute the answer when there is only one shop of each type? Find the rightmost and the leftmost coordinates. Let \(a\), \(b\) and \(c\) be the coordinates of the bread, meat and cheese shops, respectively. Clearly, the coordinate of the leftmost point is \(l = \min (a, b, c, 0)\), and of the rightmost one \(r = \max (0, a, b, c)\). To visit them optimally, we first go in one direction, return home, go in the other direction and return home again. So the answer for the case of separate shops is \((|l| + r) \cdot 2\).
What if there are many shops? We can go over the shop of each type that Zenyk visits and find the answer this way. This algorithm works in \(O(n^3)\), which is too slow. It is easy to optimize.
Consider the shops of one type. If two shops have positive coordinates, it never pays to go to the one that is farther away, so we do not need to consider it. The same reasoning holds for the shops with negative coordinates. As a result, for each type of shop we only care about two shops: the nearest one with a positive coordinate and the nearest one with a negative coordinate. After we throw away the other shops, at most 6 remain: 2 of each type. Now for each type we can try which shop we go to, since there are at most \(2 \cdot 2 \cdot 2 = 2^3 = 8\) such combinations.
The answer is the minimum of the two cases: we go either to the nearest supermarket or to a shop of each type separately.
The total complexity of the algorithm is \(O(n + 2^3)\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|