QOJ.ac

QOJ

시간 제한: 5.0 s 메모리 제한: 1024 MB 총점: 100 해킹 가능 ✓

#16917. 瓶子摆放

통계

图片由 ChatGPT 4o 生成。

Mayaw 在马太鞍部落(Fata’an Village)一家著名的 Epah(台湾原住民小米酒;Epah 是阿美族语中对台湾原住民小米酒的称呼,阿美族是台湾最大的原住民族群)酒吧工作。为了展示其丰富的收藏,酒吧里有一个双排酒架,每排恰好可以容纳 $n$ 个酒瓶。后排已经摆放了 $n$ 个酒瓶,从左到右第 $i$ 个酒瓶的高度为 $a_i$。酒吧老板还有另外 $n$ 个高度互不相同的酒瓶,高度分别为 $b_1, \dots, b_n$,他希望 Mayaw 将它们摆放在前排。为了确保酒架上的所有酒瓶都清晰可见,老板要求后排的每个酒瓶都不能被其前方的酒瓶遮挡。也就是说,如果一个高度为 $h$ 的酒瓶被放在前排的第 $i$ 个位置(从左数),那么必须满足 $h < a_i$。然而,并非所有这样的摆放方式都能让老板满意。为了向附近的 Maxi 山致敬,他额外要求前排的酒瓶高度应该呈现出“山峰”状。具体来说,前排酒瓶从左到右的高度序列必须先(非严格)单调递增,然后(非严格)单调递减。

不幸的是,有时可能无法直接满足老板的要求。因此,Mayaw 被允许通过取下高度为 $1$ 的瓶盖来略微降低酒瓶的高度。换句话说,取下瓶盖后,酒瓶的高度会恰好减少 $1$。当然,将瓶中的 Epah 暴露在空气中会损害其品质,因此应尽可能少地取下瓶盖。

你能帮 Mayaw 确定最少需要取下多少个瓶盖,以便他能以满足老板要求的方式摆放这些酒瓶吗?注意,后排酒瓶的位置是固定的,Mayaw 不能修改它们。

输入格式

第一行包含一个整数 $n$,表示每排的酒瓶数量。

第二行包含 $n$ 个整数 $a_1, \dots, a_n$,表示后排酒瓶的高度。

第三行包含 $n$ 个互不相同的整数 $b_1, \dots, b_n$,表示前排酒瓶的高度。

数据范围

  • $1 \le n \le 5 \times 10^5$
  • $1 \le a_i, b_i \le 10^9$
  • 所有的 $b_i$ 互不相同。

输出格式

输出为了让 Mayaw 能够以期望的方式摆放酒瓶,最少需要取下的瓶盖数量。如果无论取下多少个瓶盖都无法实现,则输出 -1。

样例

输入样例 1

5
2 4 6 5 4
1 2 3 4 5

输出样例 1

0

输入样例 2

5
2 3 6 5 4
1 2 3 4 5

输出样例 2

0

输入样例 3

5
6 2 6 6 6
1 2 3 4 5

输出样例 3

1

输入样例 4

5
7 2 7 7 7
1 3 4 5 6

输出样例 4

-1

输入样例 5

10
18 20 16 18 16 10 13 6 4 10
19 10 9 15 4 16 6 12 3 17

输出样例 5

4

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.