-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMin cost to build block using heap.cpp
112 lines (99 loc) · 1.68 KB
/
Min cost to build block using heap.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
110
111
112
#include"bits/stdc++.h"
using namespace std;
class minHeap{
public:
int capacity;
int *harr;
int hsize;
minHeap(int cap){
capacity = cap;
harr = new int[cap];
hsize = 0;
}
int left(int i){
return 2*i+1;
}
int right(int i){
return 2*i+2;
}
int parent(int i){
return (i-1)/2;
}
void insertkey(int key){
if(hsize == capacity){
cout<<"maximum limit reached";
return;
}
hsize++;
int i = hsize - 1;
harr[i] = key;
while(i != 0 && harr[parent(i)]>harr[i]){
swap(harr[parent(i)],harr[i]);
i = parent(i);
}
}
void swap(int &a,int &b){
int temp = a;
a = b;
b = temp;
}
void heapify(int i){
int l = left(i);
int r = right(i);
int smallest = i;
if(l < hsize && harr[l] < harr[i])
smallest = l;
if(r < hsize && harr[r] < harr[smallest])
smallest = r;
if(smallest == i){
return;
}
swap(harr[smallest],harr[i]);
heapify(smallest);
}
int extractMin(){
if(hsize == 0)
return INT_MIN;
else if(hsize == 1){
int min = harr[0];
hsize--;
heapify(0);
return min;
}else{
int min = harr[0];
swap(harr[0],harr[hsize-1]);
hsize--;
heapify(0);
return min;
}
}
int reseval(){
int min1,min2;
int sum = 0;
while(hsize != 1){
min1 = extractMin();
min2 = extractMin();
sum+=min1+min2;
insertkey(min1+min2);
}
return sum;
}
void printHeap(){
for(int i = 0;i<hsize;i++){
cout<<harr[i]<<" ";
}
}
};
int main(){
int n;
cin>>n;
minHeap m(n);
int item;
for(int i = 0;i<n;i++){
cin>>item;
m.insertkey(item);
}
m.printHeap();
cout<<endl<<m.reseval();
return 0;
}