ICode9

精准搜索请尝试: 精确搜索
首页?>?其他分享> 文章详细

D1. Equalizing by Division (easy version)

2019-08-31 15:42:55??阅读:130??来源: 互联网

标签:operations?Division?Equalizing?int?number?ai?version?array?outputCopy


The only difference between easy and hard versions is the number of elements in the array.
You are given an array a consisting of n integers. In one move you can choose any ai and divide it by 2 rounding down (in other words, in one move you can set ai:=?ai2?).
You can perform such an operation any (possibly, zero) number of times with any ai.
Your task is to calculate the minimum possible number of operations required to obtain at least k equal numbers in the array.
Don’t forget that it is possible to have ai=0 after some operations, thus the answer always exists.

Input
The first line of the input contains two integers n and k (1≤k≤n≤50) — the number of elements in the array and the number of equal numbers required.
The second line of the input contains n integers a1,a2,…,an (1≤ai≤2?105), where ai is the i-th element of a.

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

Examples
inputCopy
5 3
1 2 2 4 5
outputCopy
1
inputCopy
5 3
1 2 3 4 5
outputCopy
2
inputCopy
5 3
1 2 3 3 3
outputCopy
0

题意:要使得数组中元素出现k个相等的,可做操作,每个元素都可/2,直到出现k个相等的。
解法:把每个数都/2,分解出来,然后暴力遍历每一遍。

#include
using namespace std;
const int N=1e5+10;
int a[N];
int main()
{
  int n,k;
  scanf("%d %d",&n,&k);
  for(int i=0;i>a[i];
  vector v;
  for(int i=0;i0)
    {
      v.push_back(x);
      x/=2;
    }
  }
  int ans=1e9+10;
  for(auto res:v)
  {
    vector cnt;
    for(int i=0;ires)
      {
        x/=2;
        cur++;
      }
      if(x==res) cnt.push_back(cur);
    }
    if(cnt.size()


标签:operations,Division,Equalizing,int,number,ai,version,array,outputCopy

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有