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:=⌊ai2⌋ai:=⌊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 (1≤k≤n≤501≤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 (1≤ai≤2⋅1051≤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;
} 




原文地址:https://www.cnblogs.com/Shallow-dream/p/11516066.html

时间: 2024-07-30 14:02:08

D2 Equalizing by Division (hard version) &&D1 Equalizing by Division (easy version) (easy version)(Codeforces Round #582 (Div. 3))的相关文章

Codeforces Round #575 (Div. 3) D1. RGB Substring (easy version)

Codeforces Round #575 (Div. 3) D1 - RGB Substring (easy version) The only difference between easy and hard versions is the size of the input. You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'. You are also given a

[Codeforces Round #622 (Div. 2)] - C2. Skyscrapers (hard version) (单调栈)

[Codeforces Round #622 (Div. 2)] - C2. Skyscrapers (hard version) (单调栈) C2. Skyscrapers (hard version) time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standard output This is a harder version of the probl

Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version) 单调栈

Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version) 问题 传送门 我是参考了这篇题解传送门,然后按着思路做出了的(但大佬题解中的sumr[]数组操作我没看懂,然后自己改了改). 摘抄: 维护峰值最优 找左右边的第一个比自己小的元素,维护前缀和,找最大的峰值 l[i]:用单调栈维护左边第一个比它小的数 r[i]:用单调栈维护右边第一个比它小的数 suml[i]:左边的前缀和 sumr[i]:右边的前缀和 然后遍历一遍数组,找到

Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version)(单调栈,递推)

Codeforces Round #622 (Div. 2) C2. Skyscrapers (hard version) 题意: 你是一名建筑工程师,现给出 n 幢建筑的预计建设高度,你想建成峰状,如: 1 2 3 2 1 → 1 2 3 2 1 1 2 3 1 2 → 1 2 3 1 1 8 10 6 → 8 10 6 10 6 8 → 10 6 6 问所有建筑的高度和最大为多少. 思路: 单调递增栈栈顶存储以当前点为峰的单侧最低高度下标,另存储以每个点为峰的左右最大高度和. #includ

Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version)

传送门 题意: 给你一个长为n的仅由'R','G','B'构成的字符串s,你需要在其中找出来一个子串.使得这个子串在“RGBRGBRGBRGB........(以RGB为循环节,我们称这个串为str)”里面也是一个子串,这个子串的长度是k 可是有可能s字符串中找不到,那么这个时候就可以改变s字符串中某些位置的字母来完成任务.问最少需要改变多少个字母 题解: 主要看暴力的姿势对不对.在上一道的D1上面,我是对s字符串的每一个位置进行‘R’,‘G’,‘B’的枚举,因为如果这个子串也是str的子串的话

Codeforces Round #527 (Div. 3) 总结 A B C D1 D2 F

传送门 A 贪心的取 每个字母n/k次 令r=n%k 让前r个字母各取一次 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define rep(i, a, b) for (int i = a; i <= b; ++i) 5 6 int t, n, k; 7 8 int main() { 9 cin >> t; 10 11 while (t--) { 12 cin >

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) D2. Optimal Subsequences (Hard Version) 数据结构 贪心

D2. Optimal Subsequences (Hard Version) This is the harder version of the problem. In this version, 1≤n,m≤2?105. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems. You are given a seque

Codeforces Round #350 (Div. 2) D2 二分

五一期间和然然打的团队赛..那时候用然然的号打一场掉一场...七出四..D1是个数据规模较小的题 写了一个暴力过了 面对数据如此大的D2无可奈何 现在回来看 一下子就知道解法了 二分就可以 二分能做多少个 每次对mid求一下不够的差值 比较差值与m的大小进行l与r的变换 由于自己一向对二分比较迷茫 自己琢磨出来一套神奇的办法面对边界数据 当小于和大于的时候 抛弃mid值 当等于的时候 直接break 然后打一发while试试能否向更好的情况偏移 当然在这个题目中 如果是直接break的时候就不用

【Codeforces Round #575 (Div. 3) 】 RGB Substring (hard version) ( FFT)

D2. RGB Substring (hard version) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The only difference between easy and hard versions is the size of the input. You are given a string ss cons