-
(우선 순위 큐)1715 카드 정렬하기카테고리 없음 2022. 2. 25. 21:37
(1)1715 카드 정렬하기
https://www.acmicpc.net/problem/1715
1715번: 카드 정렬하기
정렬된 두 묶음의 숫자 카드가 있다고 하자. 각 묶음의 카드의 수를 A, B라 하면 보통 두 묶음을 합쳐서 하나로 만드는 데에는 A+B 번의 비교를 해야 한다. 이를테면, 20장의 숫자 카드 묶음과 30장
www.acmicpc.net
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<string> #include<vector> #include<algorithm> #include<queue> #include<set> using namespace std; priority_queue<int, vector<int>, greater<int>>q; //최소힙 int main() { cin.tie(NULL); cin.sync_with_stdio(false); cout.tie(NULL); int N; cin >> N; while (N--) { int card; cin >> card; q.push(card); } int total_sum = 0; while (q.size() >= 2) { int one =q.top(); q.pop(); int two = q.top(); q.pop(); int sum = 0; sum = one + two; q.push(sum); total_sum += sum; } cout << total_sum; return 0; }