-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnearlySorted.cpp
54 lines (50 loc) · 930 Bytes
/
nearlySorted.cpp
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
void nearlySorted(int arr[], int n, int k) {
priority_queue<int, vector <int>, greater<int> > pq1;
for (int i = 0; i < k + 1; i++) {
pq1.push(arr[i]);
}
int a = 0;
for (int i = k + 1; i < n; i++) {
int mini = pq1.top();
pq1.pop();
arr[a++] = mini;
pq1.push(arr[i]);
}
while (!pq1.empty()) {
arr[a++] = pq1.top();
pq1.pop();
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int k;
cin >> k;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
nearlySorted(arr, n, k);
for (auto i : arr) {
cout << i << " ";
}
cout << endl;
}
return 0;
}
/*
2
12 3
4 2 3 1 7 6 5 8 11 10 9 12
6 2
2 1 0 5 4 3
5 3
7 8 5 1 6
*/