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数组中每个数的最小操作数,找到最小

#include 
#include 
#include 
#include 
#include 
#include 
typedef long long ll;
using namespace std;
const int N = 1e+6+5;

int a[N],n,k;
vectorb;

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++){
		vectorc;
		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("%d\n",ans);
	return 0;
}

你可能感兴趣的:(Codeforces)