CF1328F.Make k Equal
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given the array a consisting of n elements and the integer k≤n.
You want to obtain at least k equal elements in the array a. In one move, you can make one of the following two operations:
- Take one of the minimum elements of the array and increase its value by one (more formally, if the minimum value of a is mn then you choose such index i that ai=mn and set ai:=ai+1);
- take one of the maximum elements of the array and decrease its value by one (more formally, if the maximum value of a is mx then you choose such index i that ai=mx and set ai:=ai−1).
Your task is to calculate the minimum number of moves required to obtain at least k equal elements in the array.
给你一个包含 n 个元素的数组 a 和一个整数 k≤n。
你希望在数组 a 中至少得到 k 个相等的元素。每次操作,你可以执行以下两种操作之一:
- 选取数组中一个最小元素,并将其值加一(更准确地说,若数组 a 的最小值为 mn,则你选择某个下标 i 满足 ai=mn,并令 ai:=ai+1);
- 选取数组中一个最大元素,并将其值减一(更准确地说,若数组 a 的最大值为 mx,则你选择某个下标 i 满足 ai=mx,并令 ai:=ai−1)。
你的任务是计算出使数组中至少出现 k 个相等元素所需的最少操作次数。
输入格式
The first line of the input contains two integers n and k (1≤k≤n≤2⋅105) — the number of elements in a and the required number of equal elements.
The second line of the input contains n integers a1,a2,…,an (1≤ai≤109), where ai is the i-th element of a.
输入的第一行包含两个整数 n 和 k(1≤k≤n≤2⋅105)—— 分别表示数组 a 的元素个数以及所需的相等元素个数。
输入的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109),其中 ai 是数组 a 的第 i 个元素。
输出格式
Print one integer — the minimum number of moves required to obtain at least k equal elements in the array.
输出一个整数——在数组中获得至少 k 个相等元素所需的最少移动次数。
输入输出样例
输入#1
6 5 1 2 2 4 2 3
输出#1
3
输入#2
7 5 3 3 2 1 1 1 3
输出#2
4
输入解题思路,AI测评打分。不知道怎么写?