forked from krishna14kant/Data-Structures-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge_K_sorted_arrays.cpp
53 lines (45 loc) · 1.04 KB
/
Merge_K_sorted_arrays.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
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
typedef pair<int,pi> pv;
#define mk make_pair
int main()
{
int n,k;
cin>>k>>n;
vector<vector<int>> a(k);
int b[n*k]={0},l=0;
int c[n];
for(int i=0;i<k;i++)
for(int j=0;j<n;j++)
{
int d;
cin>>d;
a[i].push_back(d);
}
priority_queue<pi,vector<pi>,greater<pi>> pq;
for(int i=0;i<k;i++)
{
int d = a[i][0];
pq.push(mk(d,i));
a[i].erase(a[i].begin());
}
int m;
pair<int,int> d;
while((pq.top()).second != -1){
d = pq.top();
b[l++] = d.first; // smallest element
m = d.second;
pq.pop();
if(a[m].size()==0)
pq.push(mk(INT_MAX,-1));
else
{
int e = a[m][0];
pq.push(mk(e,m));
a[m].erase(a[m].begin());
}
}
for(int i=0;i<n*k;i++)
cout<<b[i]<<" ";
}