QOJ.ac

QOJ

时间限制: 1.0 s 内存限制: 512 MB 总分: 100 可 Hack ✓

#12809. 安检

统计

在 Bytetown 机场,有两条长队正在等待安检。安检一个人需要一分钟,且两条队列可以同时进行安检。

A 队和 B 队准备乘飞机旅行。每支队伍都有 $n$ 名队员,根据他们的平均表现排名为 $1$ 到 $n$。同一支队伍中没有两名队员的排名相同。A 队在队列 1 等候,B 队在队列 2 等候。没有其他人正在等待安检。

Little Q 是负责管理这两条队列的警察。每一分钟,他可以选择检查其中一条队列的队首一人,或者同时检查两条队列的队首各一人。他不能改变队列的顺序,因为那样会引起不满。然而,还有一个额外的问题:如果两名队员 $A_i$ 和 $B_j$ 同时被检查,且他们的排名非常接近,具体来说满足 $|A_i - B_j| \le k$,他们就会制造很大的噪音。Little Q 绝不能让这种情况发生。

请编写一个程序,帮助 Little Q 找到一种安检所有人的方法,使得所需时间尽可能短。

输入格式

第一行包含两个整数 $n$ 和 $k$:每支队伍的人数和排名相似度参数 ($1 \le n \le 6 \cdot 10^4, 1 \le k \le 10$)。

第二行包含 $n$ 个不同的整数 $A_1, A_2, \dots, A_n$:第一个队列从前到后的顺序 ($1 \le A_i \le n$)。

第三行包含 $n$ 个不同的整数 $B_1, B_2, \dots, B_n$:第二个队列从前到后的顺序 ($1 \le B_i \le n$)。

输出格式

输出一行,包含一个整数:安检所有人的最小所需时间。

样例

样例输入 1

4 2
2 3 1 4
1 2 4 3

样例输出 1

7

说明

一种可能的方案如下:

第 1 分钟:检查 $A_1$。 第 2 分钟:检查 $A_2$。 第 3 分钟:检查 $A_3$。 第 4 分钟:检查 $A_4$ 和 $B_1$。 第 5 分钟:检查 $B_2$。 第 6 分钟:检查 $B_3$。 第 7 分钟:检查 $B_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.