-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEqualizing_by_Division_hard_version.cpp
More file actions
54 lines (38 loc) · 1.06 KB
/
Equalizing_by_Division_hard_version.cpp
File metadata and controls
54 lines (38 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// https://codeforces.com/contest/1213/problem/D2
#define DEBUG
#define MAXN 1e9+10
#include <bits/stdc++.h>
using namespace std;
int main ()
{
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef DEBUG
freopen("in.in", "r", stdin);
#endif
int n, k;
cin >> n >> k;
vector<int > a;
for (int i = 0; i < n; ++i) {
int temp;
cin >> temp;
a.push_back(temp);
}
// let val[x][i] be a_i divide val[x][i] times of 2 will obtain x
// so array val[x] will not consider who can obtain x, it consider how many number 'can' obtain x
vector<vector<int > > val(200000+10);
for (int i = 0; i < n; ++i) {
int x = a[i], count = 0;
while (x > 0) {
val[x].push_back(count++);
x/=2;
}
}
int ans = MAXN;
for (int i = 0; i < 200000+10; ++i) {
if(int(val[i].size()) < k) continue;
sort(val[i].begin(), val[i].end());
ans = min(ans, accumulate(val[i].begin(), val[i].begin()+k, 0));
}
cout << ans << endl;
return 0;
}