摆摊
You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.
题目描述
小张的摊位上摆着一排共n个水果,表示第i个水果的种类,小张有强迫症,想要最左边k个水果两两种类不同,交换两个相邻的水果需要花费1体力,小张想让你写个程序帮他算算最少需要消耗多少体力
输入格式
第一行,两个整数n,k;
第二行,n个整数,,...;
输出格式
一行,一个整数,若有解,输出最小体力; 否则输出-1;
输入输出样例 #1
输入 #1
5 3
3 3 3 1 2
输出 #1
4
输入输出样例 #2
输入 #2
3 2
1 1 1
输出 #2
-1
说明/提示
样例#1解释
最优方案为先交换位置 3 和 4 的水果、再交换位置 4 和 5 的水果,接着交换位置 2 和 3 的水果,最后交换位置 3 和 4 的水果,共 4 次操作。
数据范围 对于 100% 的数据, , 。
第三届吉利学院大学生程序设计竞赛
- Status
- Done
- Rule
- XCPC
- Problem
- 10
- Start at
- 2025-11-23 9:00
- End at
- 2025-11-23 12:00
- Duration
- 3 hour(s)
- Host
- Partic.
- 68