#P5844. Optimal Rice Warehouse Placement
Optimal Rice Warehouse Placement
Optimal Rice Warehouse Placement
In a rural area, there is a long straight road called the "Mi Dao". Along this road, there are (R) rice fields, each located at an integer coordinate between (1) and (L) (inclusive). The coordinates are given in non-decreasing order (i.e. (1 \le X[0] \le X[1] \le \cdots \le X[R-1] \le L)); note that more than one field may share the same coordinate.
We plan to build a rice warehouse on this road (at an integer coordinate in ([1,L])). The warehouse can be built at any valid position, including positions that already have one or more rice fields.
During the harvest season, each rice field produces exactly one truckload of rice. To transport the rice from a field to the warehouse, a truck is hired and the cost equals the absolute difference between the field's coordinate and the warehouse's coordinate. In other words, if the warehouse is built at position (P), then transporting rice from a field at position (X[i]) costs (|X[i] - P|) units.
Unfortunately, due to a limited budget of (B) units for transportation, you may not transport rice from all fields. You are allowed to select a subset of the fields to harvest such that the total transportation cost does not exceed (B). Your task is to choose a warehouse location (P) (and implicitly decide which fields to use, by taking those closest to (P)) so that the maximum number of truckloads is collected while keeping the total cost within budget.
Formally, given a sorted array (X) of (R) integers representing the positions of the rice fields and an integer (B), determine the maximum number of truckloads that can be transported if the warehouse is optimally placed and the rice is transported from a subset of fields whose total cost (\sum_{i \in S} |X[i]-P|) does not exceed (B). The optimal strategy, for any chosen (P), is to take the fields with the smallest values of (|X[i]-P|).
inputFormat
The first line of input contains three integers (R, L, B) separated by spaces, where (R) is the number of rice fields, (L) is the maximum coordinate value, and (B) is the transportation budget. The second line contains (R) integers (X[0], X[1], \dots, X[R-1]) in non-decreasing order, representing the coordinates of the rice fields.
outputFormat
Output a single integer: the maximum number of truckloads (rice fields) that can be transported to the warehouse without exceeding the budget (B).
sample
5 10 3
1 2 7 8 10
3