zoukankan      html  css  js  c++  java
  • codeforces Equalizing by Division (easy version)

    output
    standard output

    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
    题解:开一个数组cnt,保存某一个数出现的次数。然后循环除2,同时再开一个数组num,用来保存当初的数字,需要除多少次2才能变成当前的值。同时判断当前数字出现的次数,
    #include<bits/stdc++.h>
    using namespace std;
    const int N=2E5+7;
    const int inf=1e9+7; 
    int arr[N];
    int num[N];
    int cnt[N];
    int main(){
        int n,k;
        cin>>n>>k;
        for(int i=1;i<=n;i++) scanf("%d",&arr[i]);
        sort(arr+1,arr+1+n);
        int ans=inf; 
        for(int i=1;i<=n;i++){
            int temp=0;
            int x=arr[i];
            while(x){
                cnt[x]++;//判断arr[i]出现的次数;
                num[x]+=temp;//判断arr[i]编程现在的arr[i]需要操作几次;
                if(cnt[x]==k) {
                    ans=min(ans,num[x]);
                }
                temp++;
                x/=2;
            }
        }
        printf("%d",ans);
        return 0;
    } 





  • 相关阅读:
    Rhythmk 一步一步学 JAVA(4):Spring3 MVC 之 Hello Word
    使用webclient同时post普通表单字段和文件表单字段数据到指定的URL【转】
    Rhythmk 一步一步学 JAVA(2) : 操作 MYSQL 数据库
    判断FLASH版本问题
    Rhythmk 一步一步学 JAVA(3): java JSON 数据序列化 以及反序列化
    Rhythmk 一步一步学 JAVA(5) Hibernate环境配置
    JS 数据存储
    文件下载 获取远程图片
    SQL SERVER 学习笔记
    AngularJS源码学习一 :directive
  • 原文地址:https://www.cnblogs.com/Accepting/p/11458107.html
Copyright © 2011-2022 走看看