zoukankan      html  css  js  c++  java
  • D2. 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

    Print one integer — the minimum possible number of operations required to obtain at least kk equal numbers in the array.

    Examples
    input
    Copy
    5 3
    1 2 2 4 5
    
    output
    Copy
    1
    
    input
    Copy
    5 3
    1 2 3 4 5
    
    output
    Copy
    2
    
    input
    Copy
    5 3
    1 2 3 3 3
    
    output
    Copy
    0
    
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    #include <unordered_set>
    #include <unordered_map>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    using namespace std;
    int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const double eps = 1e-6;
    const int mod =1e9+7;
    const int N = 200005;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m * n / gcd(m, n);
    }
    ll qpow(ll m, ll k, ll mod)
    {
        ll res = 1, t = m;
        while (k)
        {
            if (k & 1)
                res = res * t % mod;
            t = t * t % mod;
            k >>= 1;
        }
        return res;
    }       
    int cnt[N],a[N],b[N];
    int main()
    {
        int n, x;
        cin >> n >> x;
        rep(i, 1, n)
        {
            a[i] = read();
        }
        sort(a + 1, a + n + 1);
        rep(i, 1, n)
        {
            int j = a[i];
            int op=0;
            while (j != 0)
            {
                if (b[j] >= x)
                    break;
                cnt[j] += op;
                b[j]++;
                j /= 2;
                op++;
            }
        }
        int res = inf;
        rep(i, 1, N)
        {
            if (b[i] >= x)
                res = min(res, cnt[i]);
        }
        cout << res << endl;
        return 0;
    }
     
  • 相关阅读:
    集合(set)
    字典方法
    字典(dict)
    元组(tuple)
    列表方法
    xxxx(四):接受消息hook地址分析
    xxxx(三)“黑吃黑”: 破解别人外挂
    UDP内网穿透和打洞原理与代码实现
    VMP加壳(三):VMP壳爆破实战-破解某编辑类软件
    VMP加壳(二):VMP的虚拟化原理
  • 原文地址:https://www.cnblogs.com/dealer/p/13111085.html
Copyright © 2011-2022 走看看