我是靠谱客的博主 瘦瘦缘分,这篇文章主要介绍D1. Equalizing by Division (easy version),现在分享给大家,希望可以做个参考。

The only difference between easy and hard versions is the number of elements in the array.

You are given an array a consisting of n integers. In one move you can choose any ai and divide it by 2 rounding down (in other words, in one move you can set ai:=⌊ai2⌋).

You can perform such an operation any (possibly, zero) number of times with any ai.

Your task is to calculate the minimum possible number of operations required to obtain at least k equal numbers in the array.

Don’t forget that it is possible to have ai=0 after some operations, thus the answer always exists.

Input
The first line of the input contains two integers n and k (1≤k≤n≤50) — the number of elements in the array and the number of equal numbers required.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤2⋅105), where ai is the i-th element of a.

Output
Print one integer — the minimum possible number of operations required to obtain at least k equal numbers in the array.

Examples
inputCopy
5 3
1 2 2 4 5
outputCopy
1
inputCopy
5 3
1 2 3 4 5
outputCopy
2
inputCopy
5 3
1 2 3 3 3
outputCopy
0

思路:因为这一题范围比较小,所以可以使用爆力,将n个数每个数整除2,直到0为止,将这些数存放在数组中,然后再来统计n个数每次得到b数组中每个数的最小操作数,找到最小

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h> #include <algorithm> #include <string.h> #include <vector> #include <numeric> #include <math.h> typedef long long ll; using namespace std; const int N = 1e+6+5; int a[N],n,k; vector<int>b; int main(){ scanf("%d%d",&n,&k); for(int i = 1;i <= n;i++){ scanf("%d",a+i); int x = a[i]; while(x){ b.push_back(x); x /= 2; } } int ans = 1e9; for(int j = 0;j < b.size();j++){ vector<int>c; for(int i = 1;i <= n;i++){ int x = a[i],k = 0; while(x > b[j]){ x /= 2; k++; } if(x == b[j]){ c.push_back(k); } } if(c.size() < k) continue; sort(c.begin(),c.end()); ans = min(ans, accumulate(c.begin(), c.begin() + k, 0)); } printf("%dn",ans); return 0; }

最后

以上就是瘦瘦缘分最近收集整理的关于D1. Equalizing by Division (easy version)的全部内容,更多相关D1.内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(94)

评论列表共有 0 条评论

立即
投稿
返回
顶部