思路:
题目中
Don’t forget that it is possible to have ai=0 after some operations, thus the answer always exists.
那么可以考虑用vector储存暴力枚举输入的数通过除2向下取整到0 ~ 2e^5的次数;
然后对每种情况从小到大排序,再取出前k个的和进行比较;
ACcode:
/*
author:nttttt;
add oil!
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<utility>
#include<cmath>
#include<vector>
#include<functional>//使用 greater<int>();
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10;
vector<int> a[N];
int b[N];
int main()
{
int n,k;
cin >> n >> k;
for(int i = 1; i <= n ;i++)
{
ll cnt = 0;
cin >> b[i];
while(b[i])
{
a[b[i]].push_back(cnt++);
b[i] /= 2;
}
}
ll t ,res = INF;
for(int i = 0; i <= 200010;i++)
{
t = 0;
if(a[i].size() < k) continue;
sort(a[i].begin(),a[i].end());
for(int j = 0; j < k;j++)
t += a[i][j];
res = min(res,t);
}
cout << res << endl;
return 0;
}