2019年个人训练赛第一场-E - Equalizing by Division (hard version)

The only difference between easy and hard versions is the number of elements in the array.

You are given an array aa consisting of nn integers. In one move you can choose any aiai and divide it by 22 rounding down (in other words, in one move you can set ai:=ai2ai:=⌊ai2⌋).

You can perform such an operation any (possibly, zero) number of times with any aiai.

Your task is to calculate the minimum possible number of operations required to obtain at least kk equal numbers in the array.

Don't forget that it is possible to have ai=0ai=0 after some operations, thus the answer always exists.

Input

The first line of the input contains two integers nn and kk (1kn21051≤k≤n≤2⋅105) — the number of elements in the array and the number of equal numbers required.

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai21051≤ai≤2⋅105), where aiai is the ii-th element of aa.

 

Output

The first line of the input contains two integers nn and kk (1kn21051≤k≤n≤2⋅105) — the number of elements in the array and the number of equal numbers required.

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai21051≤ai≤2⋅105), where aiai is the ii-th element of aa.

Exanple

Input
5 3
1 2 2 4 5
Output
1
Input
5 3
1 2 3 4 5
Output
2
Input
5 3
1 2 3 3 3
Output
0
简单用中文描述一下就是使一组数通过除2,来达到有k个相同的值的结果,然后问你最少要通过几步
 
思路就是创建一个优先队列,里面存放的是可以变成他的数,变成他所需要的步数,然后奖前k项相加,输出最小值就可以了
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio> 
#define INF 0x3f3f3f3f
using namespace std;

priority_queue<int,vector<int>,greater<int> > q[(int)3e5];

int main(){
    int x,y,a;
    scanf("%d%d",&x,&y);
    for(int i=0;i<x;i++){
        scanf("%d",&a);
        q[a].push(0);
        int cnt=1;
        while(a){    
            a/=2;
            q[a].push(cnt);
            cnt++;
        }
    }
    
    int asn=INF;
    for(int i=0;i < 3e5;i++){
        if(q[i].size()<y)
            continue;
        else{
            int sum=0;
            for(int j=0;j<y;j++){
                    sum=sum+q[i].top();
                    q[i].pop();
            }
            asn=min(asn, sum);
    }
}

    printf("%d\n",asn);
    return 0;
}

 



 

转载于:https://www.cnblogs.com/gbtj/p/11487809.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值