-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
59 lines (55 loc) · 1.47 KB
/
test.cpp
File metadata and controls
59 lines (55 loc) · 1.47 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
53
54
55
56
57
58
59
/*
2
8 3
1 2 2 1 3 3 2 1
5 3
1 1 2 2 3
*/
#include <bits/stdc++.h>
using namespace std;
const int SIZE = (1<<20);
long long sum[SIZE];
long long prefix(int l, int r) {
if(l > r || r < 0)
return 0;
long long ret = sum[r];
if(l > 0) ret -= sum[l-1];
return ret;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t; cin >> t;
while(t--) {
int n, k, val, pos; cin >> n >> k;
set<int> s;
int *arr = new int[n];
for(int i=0; i<n; ++i)
cin >> arr[i], s.insert(arr[i]);
vector<int> vec[SIZE];
for(int i=0; i<n; ++i)
vec[arr[i]].push_back(i);
long long ans=(1ll<<60);
for(auto &v : vec) {
if(k > v.size()) continue;
sum[0]=v[0];
sum[v.size()]=0;
for(int i=1; i<v.size(); ++i)
sum[i] = sum[i-1]+v[i];
for(int j=0; j+k<=v.size(); ++j) {
long long l = j, r = j+k-1;
long long mid = l+(r-l)/2;
long long left_cost = (mid-l)*v[mid] - ((mid-l)*(mid-l+1))/2 - prefix(l, mid-1);
long long right_cost = prefix(mid+1, r) - ((r-mid)*(r-mid+1))/2 - (r-mid)*v[mid];
ans = min(ans, left_cost+right_cost);
}
}
cout << (ans == (1ll<<60) ? -1 : ans) << "\n";
delete[] arr;
arr = nullptr;
}
return 0;
}