Skip to content

Commit

Permalink
2024-05-13 카드 정렬하기
Browse files Browse the repository at this point in the history
  • Loading branch information
oesnuj committed May 13, 2024
1 parent 5d67668 commit db33368
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions oesnuj/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
| 5차시 | 2024.04.13 | 이분탐색 | [듣보잡](https://www.acmicpc.net/problem/1764) | [#20](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/20) |
| 6차시 | 2024.05.06 | 기하학 | [정사각형](https://www.acmicpc.net/problem/1485) | [#22](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/22) |
| 7차시 | 2024.05.08 | 스택, 큐, 덱 | [queuestack](https://www.acmicpc.net/problem/24511) | [#24](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/24) |
| 8차시 | 2024.05.13 | 우선순위 큐 | [카드 정렬하기](https://www.acmicpc.net/problem/1715) | [#27](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/27) |
---
33 changes: 33 additions & 0 deletions oesnuj/우선순위 큐/1715.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <queue>

using namespace std;

int main() {
ios::sync_with_stdio(false); cin.tie(NULL);

priority_queue <int, vector<int>, greater<int>> pq; //최소 힙으로 우선순위 큐 선언
int n;
cin >> n;
for (int i = 0; i < n; i++) //우선순위 큐에 다 넣음
{
int x;
cin >> x;
pq.push(x);
}
int result = 0;
while(pq.size() > 1) //우선순위 큐에 값이 하나만 남을 때 까지 반복
{
// 우선순위 큐에서 가장 작은 두 수를 꺼내서 합침
int a = pq.top();
pq.pop();
int b = pq.top();
pq.pop();
int sum = a + b;
pq.push(sum); // 합친 결과를 우선순위 큐에 다시 넣음

result += sum; // 결과값을 누적
}
cout << result;
return 0;
}

0 comments on commit db33368

Please sign in to comment.