Equalizing by Division (hard version)

这道题好暴力啊。。。。。。

把所

有数到0的转移状态和权值都记录下来,这里就用V【i】【j】表示 1~1e6的数转化到i的步数

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define  LL long long
#define  ULL unsigned long long
#define mod 1000000007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const double eps = 1e-2;
const int maxn = 2e5 + 5;
int vis[maxn];
int a[maxn];
int n,k;
int dis[maxn];
vector V[maxn];
int main()
{
    scanf("%d%d",&n,&k);
    for(int i = 0; i < n; i++) scanf("%d",&a[i]);
    sort(a,a + n);
    for(int i = 0; i < n; i++){
      int x = a[i],cnt = 0;
      while(x){
        V[x].push_back(cnt);
        x/=2;
        cnt++;
      }
      V[0].push_back(cnt);
    }
    for(int i = 0; i < maxn; i++){
      sort(V[i].begin(),V[i].end());
    }
    int ans = INF;
    for(int i = 0; i < maxn; i++){
      //int f = a[i];
      if(V[i].size() >= k)
      ans = min(ans,accumulate(V[i].begin(),V[i].begin() + k,0));
    }
    printf("%d\n",ans);


    return 0;
}
/*50 2
72548 51391 1788 171949 148789 151619 19225 8774 52484 74830 20086 51129 151145 87650 108005 112019 126739 124087 158096 59027 34500 87415 115058 194160 171792 136832 1114 112592 171746 199013 101484 182930 185656 154861 191455 165701 140450 3475 160191 122350 66759 93252 60972 124615 119327 108068 149786 8698 63546 187913*/

 

你可能感兴趣的:(Equalizing by Division (hard version))