大致題意:給n個數,求通過操作,讓數組中的有K個相同的數的最少操作次數。操作:將數字右移一位,就是除二取整啦。感覺最近中了降智打擊,腦袋昏昏沉沉的,還有思考問題的時候像個HP一樣。。。。。。。可能是太緊張了,而且休息的不夠吧。
解題思路,枚舉一下每個數通過操作後的可能值,再用map統計一下不就完了,當然,先對原數組排序一下。最近是真的蠢,這個簡單題當時都沒有秒。。。。。。。
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+100;
int arr[maxn];
int main()
{
map<int,int>mp;
map<int,int>cnt;
int n,k;
cin>>n>>k;
for(int i=0;i<n;++i)
{
cin>>arr[i];
}
sort(arr,arr+n);
int ans=10000000;
for(int i=0;i<n;++i)
{
int tp=arr[i];
int ct=0;
while(tp)
{
mp[tp]+=ct;
++cnt[tp];
if(cnt[tp]>=k)
{
ans=min(ans,mp[tp]);
}
++ct;
tp>>=1;
}
}
cout<<ans<<endl;
return 0;
}