#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 nn, and the seat positions are numbered from 11 to nn.

Each passenger has a drink preference. There are kk types of drinks, numbered from 11 to kk. 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 pp passengers. In other words, one bottle has capacity pp. The cart can carry at most mm bottles. It is guaranteed that kmk \le m.

Passengers are served in order of seat number from 11 to nn. The cart starts moving from the starting point in front of the seats (position 00) and eventually reaches the ending point (position n+1n + 1). 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 ii, the distance to the storeroom at the starting point is ii, and the distance to the storeroom at the ending point is n+1in + 1 - i. 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 00 to n+1n + 1 in order to provide all passengers with their required drinks.

Input Format

The first line contains four integers nn, mm, kk, and pp, 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 cc, indicating the storeroom location. cc ranges from 11 to 33. c=1c=1 means the storeroom is only at the ending point; c=2c=2 means the storeroom is only at the starting point; c=3c=3 means the storerooms are at both the starting and ending points.

The third line contains nn integers aia_i, where aia_i is the type of drink required by the passenger at seat ii.

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 m=2m = 2 bottles, and each bottle contains p=1p = 1 serving. The storeroom is located at the end of the cabin.

  • First, the cart needs to load drinks 11 and 22. These will be distributed to the passengers at seats 11 and 22. The cart needs to travel a distance of 22 from the starting point 00 to position 22, so d1=2d_1=2.
  • Next, the cart needs to go to the storeroom at the end of the cabin (distance 44), load drinks 11 and 22, and then return to seat 33 (the cart needs to move a distance of 33). Thus d2=4+3=7d_2=4+3=7.
  • Then, the cart provides drinks 11 and 22 to the passengers at seats 33 and 44 (the cart moves a distance of 11 between seats 33 and 44). Thus d3=1d_3=1.
  • Finally, the cart needs to go to the storeroom again (the distance from seat 44 to the storeroom is 22), load drink 11, return from the storeroom to seat 55 (distance 11), and after serving that passenger, move a distance of 11 to the end of the cabin. Thus d4=2+1+1=4d_4=2+1+1=4.

The total distance is d1+d2+d3+d4=2+7+1+4=14d_1+d_2+d_3+d_4=2+7+1+4=14.

In the second sample, the cart can carry m=3m = 3 bottles, and each bottle contains p=2p = 2 servings. The storeroom is at the front of the cabin. The cart needs to fully load 33 bottles of drink 11 and serve the passengers at seats 11 to 44. After that, 22 bottles of drink 11 will be used up, so it needs to immediately go to the storeroom to fill 22 bottles of drink 22, and then serve the passengers at seats 55 to 88 (if it goes back to the storeroom after serving passenger 55 to refill, the travel distance will be longer).

In the third sample, the cart can carry m=3m = 3 bottles, and each bottle contains p=2p = 2 servings, with storerooms at both ends of the cabin. To serve the passengers, it needs 22 bottles of drink 22, and 11 bottle each of drinks 11 and 33, 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 22 after serving the passenger at seat 33.

In the fourth sample, the cart needs to load 11 bottle of each drink type, and since each bottle contains 22 servings, it can serve all passengers without any extra refill.

In the fifth sample, two refills are needed. After serving passenger 33, the cart needs to return to the front storeroom for the first refill, and the second refill happens after serving passenger 66, 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 100%100\% 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