Equalizing by Division (hard version)

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

把所

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

#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <numeric>
#include <ctype.h>
#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<int> 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*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值