-
Notifications
You must be signed in to change notification settings - Fork 3
/
Priority queue.cpp
109 lines (98 loc) · 2.39 KB
/
Priority queue.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Print Binary Tree levels in sorted order
Medium Accuracy: 55.08% Submissions: 1401 Points: 4
Given an array arr[] which contains data of N nodes of Complete Binary tree in level order fashion. The task is to print the level order traversal in sorted order.
Example 1:
Input:
N = 7
arr[] = {7 6 5 4 3 2 1}
Output:
7
5 6
1 2 3 4
Explanation: The formed Binary Tree is:
7
/ \
6 5
/ \ / \
4 3 2 1
Example 2:
Input:
N = 6
arr[] = {5 6 4 9 2 1}
Output:
5
4 6
1 2 9
Explanation: The formed Binary Tree is:
5
/ \
6 4
/ \ /
9 2 1
*/
vector <vector <int>> binTreeSortedLevels (int arr[], int n)
{vector<vector<int>> ans;
int height= log2(n);
priority_queue<int,vector<int>, greater<int>> pq;
int idx=0;
for(int i=0;i<=height;i++)
{
vector<int> temp;
for(int j=0;j<pow(2,i) && idx < n;j++)
{
pq.push(arr[idx]);
idx++;
}
while(pq.size() > 0)
{
temp.push_back(pq.top());
pq.pop();
}
ans.push_back(temp);
}
return ans;
// Your code here
}
};
/*
Geek and Contest ||
Easy Accuracy: 74.26% Submissions: 1128 Points: 2
Geek hosted a contest and N students participated in it. The score of each student is given by an integer array arr[]. The task is to print the number of each student (indexes) in the order they appear on the scoreboard.
A student with a maximum score appears first. If two people have the same score then a higher indexed student appears first.
Example 1:
Input:
N = 5
arr[] = {450, 230, 730, 230, 150}
Output:
3 1 4 2 5
Explanation:
1. The top scorer is at 3rd index, therefore 3
at the first index.
2. The second-highest scorer is at 1st index therefore
1 is at the second index.
3. The third-highest scorer is at 4th index therefore
4 at the thirst index.
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
priority_queue<pair<int,int>>q;
for(int i=1;i<=n;i++){
int x;
cin>>x;
q.push({x,i});
}
while(!q.empty()){
cout<<q.top().second<<" ";
q.pop();
}
cout<<endl;
}
return 0;
}