fork(3) download
  1. // Author :: Gaurav Ahirwar
  2. #include<bits/stdc++.h>
  3. #define FOR(i,N) for(int i=0;i<(N);i++)
  4. using namespace std;
  5.  
  6. struct ele {
  7. int val;
  8. int rind;
  9. int cind;
  10. };
  11. typedef struct ele ele;
  12.  
  13. class mycompare
  14. {
  15. public:
  16. bool operator()(ele n1, ele n2)
  17. {
  18. return n1.val > n2.val;
  19. }
  20. };
  21.  
  22. int n;
  23.  
  24. void solve(int arr[][4], int k) {
  25.  
  26. priority_queue<ele, vector<ele>, mycompare> heap;
  27. vector<ele> v;
  28. ele temp;
  29.  
  30. FOR(i,k) {
  31. temp.val = arr[i][0];
  32. temp.rind = i;
  33. temp.cind = 0;
  34. heap.push(temp);
  35. }
  36.  
  37. while(!heap.empty()) {
  38. temp = heap.top();
  39. heap.pop();
  40.  
  41. cout << temp.val << " ";
  42. if(temp.cind != n-1) {
  43. ele node;
  44. node.val = arr[temp.rind][temp.cind+1];
  45. node.rind = temp.rind;
  46. node.cind = temp.cind + 1;
  47. heap.push(node);
  48. }
  49. }
  50. }
  51.  
  52. int main() {
  53.  
  54. n = 4;
  55. int arr[][4] = {{2, 6, 12, 34},
  56. {1, 9, 20, 1000},
  57. {23, 34, 90, 2000}};
  58. int k = sizeof(arr)/sizeof(arr[0]);
  59. solve(arr, k);
  60.  
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
1 2 6 9 12 20 23 34 34 90 1000 2000