CF1328F.Make k Equal

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given the array aa consisting of nn elements and the integer k≤nk \le n.

You want to obtain at least kk equal elements in the array aa. 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 aa is mnmn then you choose such index ii that ai=mna_i = mn and set ai:=ai+1a_i := a_i + 1);
  • take one of the maximum elements of the array and decrease its value by one (more formally, if the maximum value of aa is mxmx then you choose such index ii that ai=mxa_i = mx and set ai:=ai−1a_i := a_i - 1).

Your task is to calculate the minimum number of moves required to obtain at least kk equal elements in the array.

给你一个包含 nn 个元素的数组 aa 和一个整数 k≤nk \le n。

你希望在数组 aa 中至少得到 kk 个相等的元素。每次操作,你可以执行以下两种操作之一:

  • 选取数组中一个最小元素,并将其值加一(更准确地说,若数组 aa 的最小值为 mnmn,则你选择某个下标 ii 满足 ai=mna_i = mn,并令 ai:=ai+1a_i := a_i + 1);
  • 选取数组中一个最大元素,并将其值减一(更准确地说,若数组 aa 的最大值为 mxmx,则你选择某个下标 ii 满足 ai=mxa_i = mx,并令 ai:=ai−1a_i := a_i - 1)。

你的任务是计算出使数组中至少出现 kk 个相等元素所需的最少操作次数。

输入格式

The first line of the input contains two integers nn and kk (1≤k≤n≤2⋅1051 \le k \le n \le 2 \cdot 10^5) — the number of elements in aa and the required number of equal elements.

The second line of the input contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \le a_i \le 10^9), where aia_i is the ii-th element of aa.

输入的第一行包含两个整数 nn 和 kk(1≤k≤n≤2⋅1051 \le k \le n \le 2 \cdot 10^5)—— 分别表示数组 aa 的元素个数以及所需的相等元素个数。

输入的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \le a_i \le 10^9),其中 aia_i 是数组 aa 的第 ii 个元素。

输出格式

Print one integer — the minimum number of moves required to obtain at least kk equal elements in the array.

输出一个整数——在数组中获得至少 kk 个相等元素所需的最少移动次数。

输入输出样例

  • 输入#1

    6 5
    1 2 2 4 2 3

    输出#1

    3
  • 输入#2

    7 5
    3 3 2 1 1 1 3

    输出#2

    4

输入解题思路,AI测评打分。不知道怎么写?

首页