-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortKSorted.cpp
More file actions
52 lines (47 loc) · 1.06 KB
/
Copy pathSortKSorted.cpp
File metadata and controls
52 lines (47 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
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define endl "\n"
#define YES "YES\n"
#define NO "NO\n"
#define ull unsigned long long
#define ll long long
#define debug() cout << "I reached here!" << endl
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pull pair<ull, ull>
#define ff first
#define ss second
#define sp " "
#define vpii vector<pii>
#define vpll vector<pll>
const int MOD = 1e9 + 7;
void solve(void){
int k;
int n; cin >> n >> k;
vi a(n, 0); for (int i = 0; i < n; i++) cin >> a[i];
// O(K)
priority_queue<int, vector<int>, greater<int>> pq(a.begin(), a.begin() + k + 1);
// O (N log K)
for (int i = 0; i < n - k - 1; i++) {
a[i] = pq.top();
pq.pop();
pq.push(a[i + k + 1]);
}
for (int i = n - k - 1; i < n; i++) {
a[i] = pq.top();
pq.pop();
}
for (auto& x : a) cout << x << sp;
cout << endl;
}
int main(void){
ios::sync_with_stdio(false);
cin.tie(NULL);
int t; cin >> t;
while (t--) solve();
return 0;
}