QOJ.ac

QOJ

时间限制: 1 s 内存限制: 512 MB 总分: 10

#2116. 橘子汽水 [B]

统计

Bajtabasz 喜欢待在家里。毕竟现在正处于疫情期间,需要保持社交距离。在电脑前度过的夜晚,除了玩《Byte Defence 3》和解决往届《Potyczki Algorytmiczne》的题目外,他最喜欢的消遣就是喝橘子汽水。Bajtabasz 的地下室里有一个长架子,上面整齐地摆放着 $n$ 瓶橘子汽水。每瓶汽水都有一个品牌,我们用整数来表示。地下室空间狭窄且地面湿滑,因此拿着瓶子走来走去很不安全——万一打碎了就不好了。正因如此,Bajtabasz 唯一能做的操作就是交换相邻的两瓶汽水。每次交换需要花费一秒钟。沿着架子走动的时间可以忽略不计。

今晚,Bajtabasz 邀请了 Bajtolina 来一起品尝橘子汽水。为了让这次活动更特别,他希望架子最左侧的 $k$ 瓶汽水品牌各不相同。

Bajtabasz 时间紧迫——他还要打扫整个房子——所以他想尽快重新排列架子上的汽水。请帮他完成这个任务!

输入格式

第一行包含两个整数 $n$ 和 $k$ ($1 \le n \le 5 \cdot 10^5$; $1 \le k \le n$),分别表示架子上的汽水总数以及为了让 Bajtabasz 高兴,最左侧必须互不相同的汽水数量。

第二行包含一个由 $n$ 个整数组成的序列 $a_1, a_2, a_3, \dots, a_n$ ($1 \le a_i \le n$),其中 $a_i$ 表示地下室架子上从左往右第 $i$ 瓶汽水的品牌。

输出格式

输出一个整数,表示使得最左侧 $k$ 瓶汽水品牌两两不同所需的最少秒数;如果无法达到这一目标,则输出 $-1$。

样例

样例输入 1

5 3
3 3 3 1 2

样例输出 1

4

样例输入 2

3 2
1 1 1

样例输出 2

-1

说明

第一个样例的解释:一种可能的交换序列如下: 3, 3, 3, 1, 2 – 初始状态 3, 3, 1, 3, 2 – 交换位置 3 和 4 的瓶子 3, 3, 1, 2, 3 – 交换位置 4 和 5 的瓶子 3, 1, 3, 2, 3 – 交换位置 2 和 3 的瓶子 * 3, 1, 2, 3, 3 – 交换位置 3 和 4 的瓶子

无法在少于 4 秒的时间内使前三瓶汽水品牌各不相同。

在第二个样例中,所有三瓶汽水品牌相同,因此我们无法使前两瓶汽水品牌各不相同。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

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.