Skip to content

Commit 32e0e73

Browse files
committed
Updated
1 parent 85dc704 commit 32e0e73

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

20- Heap/Priority_queue-II.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<bits/stdc++.h>
2+
#include<vector>
3+
using namespace std;
4+
class Person{
5+
public:
6+
string name;
7+
int age;
8+
Person(string n, int a){
9+
name=n;
10+
age=a;
11+
}
12+
};
13+
class PersonCompare{
14+
public:
15+
bool operator()(Person A, Person B){
16+
return A.age<B.age;
17+
}
18+
};
19+
20+
int main(){
21+
int n;
22+
cin>>n;
23+
priority_queue<Person,vector<int>,PersonCompare>pq;
24+
for(int i=0;i<n;i++){
25+
string name;
26+
int age;
27+
cin>>name>>age;
28+
Person p(name,age);
29+
pq.push(p);
30+
}
31+
return 0;
32+
33+
}

20- Heap/connectRopesProbelem.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int join_ropes(int ropes[],int n){
4+
priority_queue<int,vector<int>,greater<int> > pq(ropes,ropes+n);
5+
int cost=0;
6+
while(pq.size()>1){
7+
int A= pq.top();
8+
pq.pop();
9+
int B= pq.top();
10+
pq.pop();
11+
int new_rope= A+B;
12+
cost+= new_rope;
13+
pq.push(new_rope);
14+
}
15+
return cost;
16+
}
17+
int main(){
18+
int ropes[]= {4,3,2,6};
19+
int n=4;
20+
cout<<join_ropes(ropes,n)<<endl;
21+
}

20- Heap/priorty_queue.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int main(){
4+
int n;
5+
cin>>n;
6+
priority_queue<int, std::vector<int>,greater<int>>pq;
7+
for(int i=0;i<n;i++){
8+
int no;cin>>no;
9+
pq.push(no);
10+
}
11+
while(!pq.empty()){
12+
cout<<pq.top()<<" ";
13+
pq.pop();
14+
}
15+
}

0 commit comments

Comments
 (0)