QOJ.ac

QOJ

実行時間制限: 2.0 s メモリ制限: 512 MB 満点: 100 ハック可能 ✓

#18438. 删除游戏

統計

Yachiyo 拥有一个长度为 $n$ 的整数序列 $S$,序列中每个位置(从 1 开始索引)都有一个权重 $a_i$。

她可以将该序列及其权重复制并首尾相连多次。具体来说,如果她选择总共连接 $m$ 个副本($m \ge 1$),她将得到一个长度为 $m \times n$ 的新序列 $S'$ 以及对应的权重序列 $a'$。对于任意 $0 \le c < m$ 和 $1 \le i \le n$,都有 $S'_{cn+i} = S_i$ 且 $a'_{cn+i} = a_i$。

现在 Yachiyo 想要对结果序列 $S'$ 执行若干次消除操作(可能为零次)。每次操作如下:

  • 选择两个下标 $i, j$,满足 $1 \le i < j \le |S'|$ 且 $S'_i = S'_j$。
  • 删除 $S'$ 和 $a'$ 中从第 $(i+1)$ 个到第 $j$ 个的元素。
  • 操作后,剩余元素按原顺序紧密排列,总长度相应减小。

Yachiyo 想知道:在选择适当数量的副本并执行任意次数的消除操作后,剩余序列的权重之和的最小值是多少?此外,在达到该最小权重和的前提下,所需原始序列的最小副本数(即最小的 $m$)是多少?

输入格式

输入包含三行。

第一行包含一个整数 $n$ ($1 \le n \le 3 \times 10^5$),表示初始序列 $S$ 的长度。

第二行包含 $n$ 个整数 $S_1, S_2, \dots, S_n$ ($1 \le S_i \le 3 \times 10^5$),表示初始序列 $S$ 的元素。

第三行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 3 \times 10^5$),表示每个位置的权重。

输出格式

输出一行,包含两个由空格分隔的整数:

  • 第一个整数是操作后剩余序列权重之和的最小值。
  • 第二个整数是达到该最小权重和所需的原始序列的最小总副本数。

样例

样例输入 1

6
1 1 4 5 1 4
1 9 1 9 8 10

样例输出 1

2 1

说明

在本样例中,最优策略是仅使用 1 个原始序列副本(即 $m=1$,不进行额外复制)。

初始序列 $S'$ 为 $[1, 1, 4, 5, 1, 4]$,对应的权重序列 $a'$ 为 $[1, 9, 1, 9, 8, 10]$。

Yachiyo 可以执行以下两次操作:

  1. 选择 $i = 1, j = 2$(此时 $S'_1 = S'_2 = 1$),删除第 $(i+1)$ 到第 $j$ 个元素(即删除第 2 个元素)。操作后,$S'$ 变为 $[1, 4, 5, 1, 4]$,$a'$ 变为 $[1, 1, 9, 8, 10]$。
  2. 在新序列中,选择 $i = 2, j = 5$(此时 $S'_2 = S'_5 = 4$),删除第 $(i+1)$ 到第 $j$ 个元素(即删除第 3、4、5 个元素)。操作后,剩余的 $S'$ 为 $[1, 4]$,剩余的 $a'$ 为 $[1, 1]$。

此时,无法再进行进一步消除,剩余权重之和为 $1 + 1 = 2$。可以证明,在任意数量的副本和任意操作序列下,权重之和不可能小于 2。

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.