zoukankan      html  css  js  c++  java
  • D2 Equalizing by Division (hard version) &&D1 Equalizing by Division (easy version) (easy version)(Codeforces Round #582 (Div. 3))

    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 (1kn501≤k≤n≤50) — 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

    map vector 的嵌套使用(推荐),当然二维数组也可以的。
     

    
    

    #include <bits/stdc++.h>
    using namespace std;

    #define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #define add(x) push_back(x);
    #define allint int n,m,k=0,t,l=0,r=0,cnt=0,ans=0,pos=0;
    int main()
    {
        map<int,vector<int> >mv;
        allint;TLE;
        cin>>n>>k;
        for(int i=0;i<n;i++)
        {
            cin>>m;
            pos=0;    //记录有多少个数 >>1 之后等于m
            while(m)
            {
                mv[m].add(pos);  //加入当前m值之后的数中有pos个数经过多次/2后等于此时的m
                pos++;
                m>>=1;
            }
        }
        ans = 1e9;
        for(map<int,vector<int> >::iterator it=mv.begin();it!=mv.end();it++)
        {
            vector<int>arr;        
            arr=it->second;       
            if(arr.size()<k) continue;
            sort(arr.begin(),arr.end()); 
            cnt = 0;
            for(int i=0;i<k;i++)
                cnt+=arr[i];
            ans = min( ans,cnt );
        }
        cout<<ans<<endl;
        ok;
    } 



    
    
    所遇皆星河
  • 相关阅读:
    bzoj 2618: [Cqoi2006]凸多边形
    BZOJ 4556 [Tjoi2016&Heoi2016]字符串
    BZOJ 4850 [Jsoi2016]灯塔
    BZOJ 2956: 模积和
    PHP 正则表达式
    Linux Centos6.5安装redis3.0 和phpredis
    linux 删除过期文件
    THINKPHP报错 _STORAGE_WRITE_ERROR
    THINKPHP 部署nginx上URL 构造错误
    Linux 修改mysql密码
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/11516066.html
Copyright © 2011-2022 走看看