#DSU0000082. Sắp xếp các chai rượu (Bottle Arrangement)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sắp xếp các chai rượu (Bottle Arrangement)
Bottle Arrangement
Source: Codeforces 2041J – 2024 ICPC Asia Taichung Regional Contest
Version: Phuoc Hung OJ Extended
Problem Statement
A wine display rack contains two parallel rows, each with exactly positions numbered from left to right.
The rear row is already filled. Its bottle at position has height . Neither the positions nor the heights of the rear-row bottles may be changed.
You are given additional bottles to arrange in the front row, with original heights . All are distinct. You may permute these bottles arbitrarily, but every bottle must be used exactly once and every front-row position must be occupied.
An arrangement of final front-row heights is valid only if both conditions hold:
1. The rear row remains visible. At every position , the front bottle must be strictly shorter than the corresponding rear bottle:
2. The front row forms a mountain. There must exist a position such that the heights are non-decreasing up to and non-increasing afterwards:
$$h_1\le h_2\le\cdots\le h_p\ge h_{p+1}\ge\cdots\ge h_n.$$Equal adjacent heights are allowed, and the peak may occur at either endpoint.
If the original bottle heights do not admit a valid arrangement, you may remove the cap of some front-row bottles. Each bottle has at most one removable cap, and removing it decreases that bottle's height by exactly one. You may either remove the cap or leave it in place for each bottle; no bottle may be shortened more than once. The rear row may not be modified.
For example, a bottle whose original height is can be placed at height or , and at no other height. Removing a cap does not remove the bottle itself.
Find the minimum number of caps that must be removed to obtain a valid arrangement. If no valid arrangement exists even after choosing caps freely, output -1.
Input
- The first line contains a single integer , the number of bottles in each row.
- The second line contains integers , the fixed heights in the rear row.
- The third line contains distinct integers , the original heights of the front-row bottles.
Output
Print one integer: the minimum number of caps to remove, or -1 if no valid arrangement exists.
Subtask
- Subtask 1 (20 points): ; ; all are pairwise distinct.
- Subtask 2 (30 points): ; ; all are pairwise distinct.
- Subtask 3 (50 points): ; ; all are pairwise distinct.
Examples
Example 1
Input
5
2 4 6 5 4
1 2 3 4 5
Output
0
Explanation: One valid front-row arrangement has heights . This sequence first increases and then decreases, and every front bottle is strictly shorter than its corresponding rear bottle. No cap needs to be removed.
Example 2
Input
5
6 2 6 6 6
1 2 3 4 5
Output
1
Explanation: The second rear bottle has height , forcing the front bottle at that position to have height less than . Remove the cap from the bottle originally of height , reducing it to , and arrange the front row as . This forms a mountain and satisfies every strict rear-height bound. Keeping all caps in place is not sufficient.
Example 3
Input
5
7 2 7 7 7
1 3 4 5 6
Output
-1
Explanation: Only the originally shortest bottle can be made short enough for position . The other bottles cannot be arranged to form a mountain with this position constrained to be that low, so no valid arrangement exists.