ICode9

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

D1. Equalizing by Division (easy version)

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

标签: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<bits/stdc++.h>
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<n;i++) cin>>a[i];
  vector<int> v;
  for(int i=0;i<n;i++)
  {
    int x=a[i];
    while(x>0)
    {
      v.push_back(x);
      x/=2;
    }
  }
  int ans=1e9+10;
  for(auto res:v)
  {
    vector<int> cnt;
    for(int i=0;i<n;i++)
    {
      int x=a[i];
      int cur=0;
      while(x>res)
      {
        x/=2;
        cur++;
      }
      if(x==res) cnt.push_back(cur);
    }
    if(cnt.size()<k) continue;
    sort(cnt.begin(),cnt.end());
    ans=min(ans,accumulate(cnt.begin(),cnt.begin()+k,0));
  }
  printf("%d\n",ans);
}

标签:operations,Division,Equalizing,int,number,ai,version,array,outputCopy
来源: https://blog.csdn.net/qq_43690454/article/details/100173379

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

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

ICode9版权所有