#P11114. [ROI 2024] 小推车 (Day 1)
[ROI 2024] 小推车 (Day 1)
Background
Translated from ROI 2024 D1T3.
Problem Description
An airline offers a new type of business class, where the seats on the plane are arranged along the aisle. Suppose the total number of seats is , and the seat positions are numbered from to .
Each passenger has a drink preference. There are types of drinks, numbered from to . Each passenger chooses exactly one drink. On the plane, drinks must be distributed to passengers from a small cart.
Each drink is stored in bottles. One bottle can serve passengers. In other words, one bottle has capacity . The cart can carry at most bottles. It is guaranteed that .
Passengers are served in order of seat number from to . The cart starts moving from the starting point in front of the seats (position ) and eventually reaches the ending point (position ). While serving passengers, the cart may need to go to a storeroom to load new drinks. The storeroom may be located at the front starting point, or at the rear ending point, or at both ends.
If the cart is at seat position , the distance to the storeroom at the starting point is , and the distance to the storeroom at the ending point is . During the trip, the cart may need to refill drinks. If the cart reaches a storeroom, it can unload empty bottles and load new ones. It is not allowed to unload bottles that still contain drinks. After refilling, the cart should return to the position of the first passenger who has not been served yet, and then continue serving forward.
You need to compute the minimum total distance the cart moves from to in order to provide all passengers with their required drinks.
Input Format
The first line contains four integers , , , and , representing the number of seats, the cart's maximum number of bottles, the number of drink types, and the capacity of each bottle.
The second line contains an integer , indicating the storeroom location. ranges from to . means the storeroom is only at the ending point; means the storeroom is only at the starting point; means the storerooms are at both the starting and ending points.
The third line contains integers , where is the type of drink required by the passenger at seat .
Output Format
Output one integer, the minimum total distance the cart needs to move while serving all passengers.
5 2 2 1
1
1 2 1 2 1
14
8 3 2 2
2
1 1 1 1 1 2 2 2
17
8 3 3 2
3
1 2 2 3 2 3 2 1
15
8 6 6 2
2
1 2 3 4 3 5 6 1
9
7 3 3 1
3
1 2 3 2 2 1 3
16
Hint
In the first sample, the cart can carry bottles, and each bottle contains serving. The storeroom is located at the end of the cabin.
- First, the cart needs to load drinks and . These will be distributed to the passengers at seats and . The cart needs to travel a distance of from the starting point to position , so .
- Next, the cart needs to go to the storeroom at the end of the cabin (distance ), load drinks and , and then return to seat (the cart needs to move a distance of ). Thus .
- Then, the cart provides drinks and to the passengers at seats and (the cart moves a distance of between seats and ). Thus .
- Finally, the cart needs to go to the storeroom again (the distance from seat to the storeroom is ), load drink , return from the storeroom to seat (distance ), and after serving that passenger, move a distance of to the end of the cabin. Thus .
The total distance is .
In the second sample, the cart can carry bottles, and each bottle contains servings. The storeroom is at the front of the cabin. The cart needs to fully load bottles of drink and serve the passengers at seats to . After that, bottles of drink will be used up, so it needs to immediately go to the storeroom to fill bottles of drink , and then serve the passengers at seats to (if it goes back to the storeroom after serving passenger to refill, the travel distance will be longer).
In the third sample, the cart can carry bottles, and each bottle contains servings, with storerooms at both ends of the cabin. To serve the passengers, it needs bottles of drink , and bottle each of drinks and , so it must go to a storeroom to replace empty bottles. The best plan is to go to the front storeroom to exchange for drink after serving the passenger at seat .
In the fourth sample, the cart needs to load bottle of each drink type, and since each bottle contains servings, it can serve all passengers without any extra refill.
In the fifth sample, two refills are needed. After serving passenger , the cart needs to return to the front storeroom for the first refill, and the second refill happens after serving passenger , when it goes to the rear storeroom.
The original problem has twelve subtasks, but due to Luogu limits they cannot all be uploaded here.
Constraints: for of the data, $3\le n\le 10^6,1\le p\le 10^6,1\le a_i\le k\le m\le 10^6,1\le c\le3$。
Translated by ChatGPT 5