-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fba824c
commit 0fdd4ed
Showing
13 changed files
with
590 additions
and
7 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define pb push_back | ||
|
||
class Graph{ | ||
//No of vertices | ||
int v; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
vector<int>HEAP; | ||
void insert(int x,int ind){ | ||
if(HEAP[0]==0){ | ||
HEAP[ind]=x; | ||
HEAP[0]+=1; | ||
}else{ | ||
HEAP[0]+=1; | ||
HEAP[ind]=x; | ||
// elem/2 is its parent | ||
bool done = false; | ||
while(!done && ind>1){ | ||
if(HEAP[ind/2]>=HEAP[ind]){ | ||
// We are done | ||
done = 1; | ||
}else{ | ||
swap(HEAP[ind],HEAP[ind/2]); | ||
if(ind==1) | ||
done=1; | ||
ind = ind/2; | ||
} | ||
} | ||
} | ||
} | ||
|
||
int maxim(){ | ||
if(HEAP[0]==0) | ||
return -1; | ||
return HEAP[1]; | ||
} | ||
|
||
void heapify(){ | ||
bool done = false; | ||
int ind = 1; | ||
while(!done){ | ||
if(HEAP[ind]>=HEAP[2*ind] && HEAP[ind]>=HEAP[2*ind+1]){ | ||
done = 1; | ||
}else{ | ||
if(HEAP[2*ind]>=HEAP[2*ind+1]){ | ||
swap(HEAP[ind],HEAP[2*ind]); | ||
ind = 2*ind; | ||
}else{ | ||
swap(HEAP[ind],HEAP[2*ind+1]); | ||
ind = 2*ind+1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
void deleteMaxim(){ | ||
if(HEAP[0]==0) | ||
return ; | ||
|
||
swap(HEAP[HEAP[0]],HEAP[1]); | ||
HEAP[HEAP[0]]=0; | ||
HEAP[0]-=1; | ||
heapify(); | ||
} | ||
|
||
int main(int argc, char const *argv[]){ | ||
|
||
int N,temp,x; | ||
cin>>N; | ||
HEAP.resize(N+1); | ||
|
||
x=N; | ||
int elem = 1; | ||
while(x--){ | ||
cin>>temp; | ||
insert(temp,elem); | ||
elem+=1; | ||
} | ||
cout<<"YOUR HEAP IS READY: "<<'\n'; | ||
for(auto e:HEAP) | ||
cout<<e<<' '; | ||
cout<<'\n'; | ||
|
||
x=N; | ||
while(x--){ | ||
cout<<maxim()<<'\n'; | ||
deleteMaxim(); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/* | ||
5 | ||
4 3 | ||
2 1 -1 -4 | ||
0 1 2 3 4 5 6 | ||
5 4 3 2 1 -1 -4 | ||
5---2 | ||
4---1 | ||
ceil(5/2)-1 | ||
ceil(3/2)-1 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include <bits/stdc++.h> | ||
#define pb push_back | ||
using namespace std; | ||
|
||
struct job{ | ||
int id; | ||
int p; | ||
job(){}; | ||
job(int a,int b):id(a),p(b){}; | ||
}; | ||
vector<job>ARR; | ||
|
||
|
||
void siftup(int ind){ | ||
while(ind>1){ | ||
int par = ind/2; | ||
if(ARR[par].p<ARR[ind].p){ | ||
swap(ARR[par],ARR[ind]); | ||
ind = par; | ||
}else | ||
break; | ||
|
||
} | ||
} | ||
job getMax(){ | ||
if(ARR.size()>1) | ||
return ARR[1]; | ||
return job(-1,-1); | ||
} | ||
void insert(int id,int p){ | ||
job nJob = job(id,p); | ||
ARR.pb(nJob); | ||
siftup(ARR.size()-1); | ||
} | ||
|
||
job extractMax(){ | ||
if(ARR.size()<=1) | ||
return job(-1,-1); | ||
|
||
job t = ARR[1]; | ||
swap(ARR[ARR.size()-1],ARR[1]); | ||
ARR.pop_back(); | ||
|
||
int ind = 1; | ||
while((2*ind<ARR.size() && 2*ind+1<ARR.size()) && (ARR[ind].p<ARR[2*ind].p || ARR[ind].p<ARR[2*ind+1].p)){ | ||
if(ARR[2*ind].p>ARR[2*ind+1].p){ | ||
swap(ARR[2*ind],ARR[ind]); | ||
ind = 2*ind; | ||
}else{ | ||
swap(ARR[2*ind+1],ARR[ind]); | ||
ind = 2*ind; | ||
} | ||
} | ||
if(2*ind<ARR.size()) | ||
if(ARR[2*ind].p>ARR[ind].p) | ||
swap(ARR[2*ind],ARR[ind]); | ||
|
||
return t; | ||
} | ||
void display(){ | ||
|
||
for(int i=1;i<ARR.size();i++){ | ||
cout<<ARR[i].id<<' '<<ARR[i].p<<'\n'; | ||
} | ||
} | ||
int main(int argc, char const *argv[]){ | ||
ARR.pb(job(-1,-1)); | ||
int ch; | ||
job t; | ||
int id,p; | ||
while(1){ | ||
cout<<"1. Extract Max\n"; | ||
cout<<"2. Get Max\n"; | ||
cout<<"3. Insert a job\n"; | ||
cin>>ch; | ||
switch(ch){ | ||
case 1: | ||
t = extractMax(); | ||
cout<<"JOB EXTRACTED. HEAP MODIFIED\n"; | ||
cout<<t.id<<' '<<t.p<<"\n\n"; | ||
display(); | ||
break; | ||
case 2: | ||
t = getMax(); | ||
cout<<t.id<<' '<<t.p<<'\n'; | ||
display(); | ||
break; | ||
case 3: | ||
cin>>id>>p; | ||
insert(id,p); | ||
cout<<"JOB INSERTED\n"; | ||
display(); | ||
break; | ||
default: | ||
break; | ||
|
||
} | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
vector<int>ARR; | ||
|
||
void heapify(int ind){ | ||
|
||
while((2*ind<ARR.size() && 2*ind+1<ARR.size()) && (ARR[2*ind]>ARR[ind] || ARR[2*ind+1]>ARR[ind])){ | ||
if(ARR[2*ind]>ARR[2*ind+1]){ | ||
swap(ARR[ind],ARR[2*ind]); | ||
ind = 2*ind; | ||
}else{ | ||
swap(ARR[ind],ARR[2*ind+1]); | ||
ind = 2*ind+1; | ||
} | ||
} | ||
if(2*ind<ARR.size() && ARR[2*ind]>ARR[ind]){ | ||
swap(ARR[ind],ARR[2*ind]); | ||
} | ||
} | ||
|
||
int getMax(){ | ||
if(ARR.size()<=1) | ||
return -1; | ||
return ARR[1]; | ||
} | ||
|
||
void extractMax(){ | ||
if(ARR.size()<=1) | ||
return ; | ||
|
||
swap(ARR[ARR.size()-1],ARR[1]); | ||
ARR.pop_back(); | ||
int ind = 1; | ||
|
||
while((2*ind<ARR.size() && 2*ind+1<ARR.size()) && (ARR[2*ind]>ARR[ind] || ARR[2*ind+1]>ARR[ind])){ | ||
if(ARR[2*ind]>ARR[2*ind+1]){ | ||
swap(ARR[ind],ARR[2*ind]); | ||
ind = 2*ind; | ||
}else{ | ||
swap(ARR[ind],ARR[2*ind+1]); | ||
ind = 2*ind+1; | ||
} | ||
} | ||
if(2*ind<ARR.size() && ARR[2*ind]>ARR[ind]){ | ||
swap(ARR[ind],ARR[2*ind]); | ||
} | ||
} | ||
void heapSort(){ | ||
int xx = 1; | ||
while(1){ | ||
xx = getMax(); | ||
extractMax(); | ||
if(xx==-1) | ||
break; | ||
cout<<xx<<' '; | ||
} | ||
} | ||
|
||
void heapifyUtil(){ | ||
for(int i=(((int)ARR.size()-1)/2);i>=1;i--){ | ||
heapify(i); | ||
} | ||
} | ||
int main(int argc, char const *argv[]){ | ||
|
||
|
||
int N; | ||
cin>>N; | ||
ARR.resize(N+1); | ||
ARR[0]=-1; | ||
|
||
for(int i=1;i<ARR.size();i++) | ||
cin>>ARR[i]; | ||
|
||
heapifyUtil(); | ||
heapSort(); | ||
|
||
|
||
for(int i=1;i<ARR.size();i++) | ||
cout<<ARR[i]<<' '; | ||
cout<<'\n'; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
vector<int>HEAP; | ||
|
||
bool check(int node){ | ||
|
||
int lchild = 2*node; | ||
int rchild = 2*node+1; | ||
|
||
|
||
if(HEAP[node]<HEAP[lchild] || HEAP[node]<HEAP[rchild]) | ||
return false; | ||
if(lchild<HEAP.size() && HEAP[node]<HEAP[lchild]) | ||
return false; | ||
else if(lchild<HEAP.size() && HEAP[node]>HEAP[lchild]) | ||
return true; | ||
else if(lchild >=HEAP.size() && rchild>=HEAP.size()) | ||
return true; | ||
if(!check(lchild)) | ||
return false; | ||
if(!check(rchild)) | ||
return false; | ||
|
||
return true; | ||
} | ||
int main(){ | ||
int N; | ||
cin>>N; | ||
HEAP.resize(N+1); | ||
priority_queue<int,vector<int>,greater<int>>pq; | ||
for(int i=1;i<=N;i++){ | ||
cin>>HEAP[i]; | ||
} | ||
|
||
if(check(1)) | ||
cout<<"TRUE\n"; | ||
else | ||
cout<<"FALSE\n"; | ||
return 0; | ||
} |
Binary file not shown.
Oops, something went wrong.