Water Delivery
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
After skiing, ice skating, and other physical activities that the couple enjoyed on Valentine’s Day, it was time for the hot tub.
Unfortunately, due to repairs, the water was cut off in the village where Zenyk and Marichka were staying. Mrs. Tarnavska is in despair! How can she fill the hot tub for her dear guests? And how can the other villagers live without water?
Fortunately, Zenyk used to work as the director of the water utility, and Marichka — as a logistics manager. Zenyk suggested that they deliver the water using Mrs. Tarnavska’s cars (whose cars did you think?). The idea came from Zenyk, the cars from Mrs. Tarnavska, and the process management from Marichka.
There is one street in the village. Let’s represent the street as a coordinate line.
On this line, there are \(n\) houses located at points with coordinates \(h_i\) kilometers. The water demand of the \(i\)-th house is \(a_i\) liters of water.
There are also \(m\) stations with an infinite supply of water, located at points with coordinates \(s_i\) kilometers. At each station, there is a car with a tank of capacity \(k\) liters, which can load water at this station and deliver it to the houses. A car can load as much water as it wants (no more than fits in the tank) as many times as it wants only at its own station. Water can be brought to any house from any station. After delivery, each car must return to its station.
Marichka needs to minimize the total distance traveled by the cars to cover the residents’ needs and return to their stations. She hasn’t worked in logistics for a long time and forgot what to do.
Help Marichka — find the minimum total distance the cars will travel.
Input
The first line contains three integers \(n\), \(m\), \(k\) — the number of houses, the number of stations, and the tank capacity of each car in liters.
The next \(n\) lines contain two integers \(h_i\) and \(a_i\) — the coordinate in kilometers of the corresponding house and the water demand in liters of the residents of this house.
The next line contains \(m\) integers \(s_i\) — the coordinates in kilometers of the water stations.
Output
In a single line, print an integer — the minimum total distance in kilometers that the cars must travel to deliver water to the houses and return to their stations.
Constraints
\(1 \le n, m \le 10^3\),
\(1 \le k, a_i \le 10^9\),
\(0 \le h_i, s_i \le 10^6\),
\(h_i < h_{i+1}\),
\(s_i < s_{i+1}\).
10 points:
\(1 \le n, m \le 100\),
\(1 \le k, a_i \le 100\),
\(0 \le h_i, s_i \le 10^3\);
15 points: without additional constraints.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 9 2 6 4 4 7 4 11 1 13 1 20 10 27 1 29 1 30 47 47 47 10 30 | 334 |
Notes
The car from the first station carries 4 liters of water to the first house and returns to the station. The distance from the station to the house is 6 km. The car covers it in both directions, so it will travel 12 km.
From the first station, it carries 4 liters to the second house and returns to the station — travels 6 km.
From the first station, 1 liter to the third house — travels 2 km.
From the first station, 1 liter to the fourth house, 5 liters to the fifth house — 20 km.
From the second station, 1 liter to the sixth house, 5 liters to the fifth house — 20 km.
From the second station, 1 liter to the seventh house — 2 km.
The needs of the eighth house can be met with water from the second station by traveling 0 km.
To satisfy the needs of the ninth house, the car from the second station must make 8 trips of 34 km. \(8 \cdot 34 = 272\) (km).
\(12+6+2+20+20+2+0+272=334\) (km).
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Water Delivery - Editorial
Consider two neighboring stations \(j\) and \(j+1\). The houses with indices from \(l\) to \(r\) are between them.
Let’s state and prove the following claim. For some optimal solution there is an index \(s\), \(l \le s \le r\), such that the needs of the \(i\)-th house are fully met with water from the \(j\)-th station for \(l \le i < s\), fully met with water from the \((j+1)\)-th station for \(s < i \le r\), and the needs of the \(s\)-th house are met partly from the \(j\)-th and partly from the \((j+1)\)-th station (possibly fully from one of them).
Proof. Suppose that water from the \(t\)-th station, \(t<j\), was brought to the \(i\)-th house, where \(l \le i \le r\). This does not pay off, since this water could have been brought from the \(j\)-th station, and the total distance would be smaller. Similarly, it does not pay to bring water from the \(t\)-th station if \(t>j+1\).
So it pays to meet the needs of all houses between stations \(j\) and \(j+1\) with water from these stations.
Let \(i_1\) be the rightmost house that got water from the \(j\)-th station, and \(i_2\) the leftmost one that got water from the \((j+1)\)-th station.
Suppose that \(i_2<i_1\). Let \(c_1\) liters of water be brought to house \(i_1\) from the \(j\)-th station, and \(c_2\) liters to house \(i_2\) from the \((j+1)\)-th station. Let \(d=\min\{c_1, c_2\}, c_1’=c_1-d, c_2’=c_2-d\).
If \(c_1’\) liters are brought to house \(i_1\) from the \(j\)-th station and \(c_2’\) liters to house \(i_2\) from the \((j+1)\)-th station, and the missing \(d\) liters for houses \(i_1\) and \(i_2\) are covered by the \((j+1)\)-th and the \(j\)-th stations, respectively, the total distance decreases, since \(c_1’\) or \(c_2’\) equals zero.
So \(i_1 \le i_2\).
The needs of the houses with numbers less than \(i_1\) are met by the \(j\)-th station, and those greater than \(i_1\) by the \((j+1)\)-th station. If \(i_1=i_2\), the needs of house \(i_1\) are met by both stations. So we can take \(s=i_1\).
This proves the claim.
It follows that the gaps between neighboring stations can be solved independently of each other. The problem splits into subproblems with two stations \(j\) and \(j+1\) and the houses between them. Let’s learn to solve such a subproblem.
A solution in \(O(\sum_{i} a_i)\).
We use dynamic programming.
Let \(dp_1[x]\) be the minimum distance the truck from the left station has to drive to bring \(x\) liters of water to the houses. \(x\) uniquely determines how much water the truck brings to which houses. Let \(i\) be the rightmost house the left truck brings water to. Then \(dp_1[x] = dp_1[\max\{0, x - k\}]+2(h_i-s_j)\).
The same DP \(dp_2\) can be computed for the right station. The answer to the subproblem is \(\min_x dp_1[x]+dp_2[t-x]\), where \(t=\sum_{i} a_i\) is the total need of the houses.
A solution in \(O(n^2 \log n)\).
Consider the number line where the number \(x\) stands for \(x\) liters of water. The \(l\)-th house corresponds to the half-interval \(\left(0, a_l \right]\), the \((l+1)\)-th to \(\left(a_l, a_l+a_{l+1} \right]\), the \((l+2)\)-th to \(\left(a_l+a_{l+1}, a_l+a_{l+1}+a_{l+2} \right]\), and so on. Let \(e_i\) be the end of the half-interval of the \(i\)-th house. Each house also has the number \(f_i=2(h_i-s_j)\), the distance the truck has to drive from the \(j\)-th station to the house and back. The DP \(dp_1\) from the previous solution can be pictured on this line: the last trip of the truck is a jump from the point \(x-k\) to the point \(x\), and for this jump we pay \(f_i\), where \(i\) is the house whose half-interval contains the point \(x\). Delivering water with the left truck can be represented by the sequence of points \(w, w + k, w + 2 \cdot k, \dots, w + p \cdot k\): in the first trip we bring \(w \le k\) liters, and in each next one \(k\) liters.
If \(w<k\) and none of the points \(w+p \cdot k\) is the end of a house’s half-interval, we can move every point of the sequence one to the right, and the total distance does not increase (each point stays in its half-interval), while the amount of water delivered (the last point of the sequence) increases by one. So we only need to consider \(w\) such that \(w=k\) or \(w+p \cdot k=e_i\) for some \(p\), \(i\), that is, \(w \equiv e_i \mod k\): there are \(O(n)\) different candidates for \(w\).
The next observation: if the left truck brought a full tank of water to the \(i\)-th house, it keeps bringing full tanks there as long as it can.
For each \(w\) we have the following candidates for the boundary between the left and the right trucks.
We fully meet the needs of the houses up to the \((i-1)\)-th one and bring the remaining water to the \(i\)-th house. The last point is the smallest \(x\) with \(e_{i-1} < x \le e_i\), \(x \equiv w \mod k\). Note that there may be no such point. Call such a candidate a type A candidate.
We fully meet the needs of the houses up to the \((i-1)\)-th one and bring the remaining water to the \(i\)-th house, bringing full tanks to the \(i\)-th house while they fit. The last point is the largest \(x\) with \(e_{i-1}+k < x \le e_i\), \(x \equiv w \mod k\). There may be no such point. Call such a candidate a type B candidate.
For each \(w\), the answer is easy to update when moving between neighboring candidates. If candidate \(y\) falls into the half-interval of the \(i\)-th house and the previous candidate was \(x\), then \(\frac{y-x}{k}f_i\) is added to the answer (the number \(\frac{y-x}{k}\) is an integer).
We compute the answers for all candidates for the left and the right trucks.
To find the answer, for each candidate \(x\) for the left truck we need to find the smallest candidate \(y\) for the right truck such that \(x+y \ge t\), where \(t=\sum_{i} a_i\) is the total need of the houses.
This can be done by sorting the candidates.
The sorting takes \(O(n^2 \log n)\).
A solution in \(O(n^2)\).
Sort all candidates for \(w\). Note that each half-interval has at most two candidates with the same remainder modulo \(k\) (at most one of type A and at most one of type B with this remainder). We want to sort the candidates in the \(i\)-th half-interval in \(O(n)\). First, find the smallest type A candidate in this half-interval. Let its remainder modulo \(k\) be \(z\). We add to the sorted list the type A candidates for all remainders \(w\) from \(z\) to \(k-1\), then from 0 to \(z-1\). For this we go over the list of candidates for \(w\). This way we sort the type A candidates. We do the same with the type B candidates. So all candidates are sorted.
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|