본문 바로가기
문제/백준

c++ 백준_1715_"카드 정렬하기"

by 나 진짜 못차마 2023. 6. 15.
728x90

https://www.acmicpc.net/problem/1715

 

1715번: 카드 정렬하기

정렬된 두 묶음의 숫자 카드가 있다고 하자. 각 묶음의 카드의 수를 A, B라 하면 보통 두 묶음을 합쳐서 하나로 만드는 데에는 A+B 번의 비교를 해야 한다. 이를테면, 20장의 숫자 카드 묶음과 30장

www.acmicpc.net

 

우선순위큐를 사용해

작은 것 두개를 뽑아서 더하고 다시 push를

반복했다.

카드묶음이 한개 뿐인 경우에는

비교할 게 없으므로 0 출력했다.

처음에 예제 입력 1을 보고

저게 70이지 왜 100이지 생각했다..

코딩할수록 점점 국어가 딸리는 거 같다.

 

#include <iostream>
#include <queue>
using namespace std;

int main(void) {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	priority_queue<int, vector<int>, greater<int>> pq;

	int N, num;
	cin >> N;

	for (int i = 0; i < N; i++) {
		cin >> num;
		pq.push(num);
	}


	int total = 0;
	if (pq.size() == 1) {
		cout << 0;
		return 0;
	}
	while (!pq.empty()) {
		
		if (pq.size() > 2) {
			int temp = pq.top(); pq.pop();
			temp += pq.top(); pq.pop();

			total += temp;

			pq.push(temp);
		}
		else if (pq.size() == 2) {
			int temp = pq.top(); pq.pop();
			temp += pq.top(); pq.pop();

			total += temp;
		}
		else {
			total += pq.top(); pq.pop();
		}

	}
	cout << total;
}

728x90

'문제 > 백준' 카테고리의 다른 글

c++ 백준_1600_"말이 되고픈 원숭이"  (0) 2023.06.15
c++ 백준_1405_"미친 로봇"  (0) 2023.06.15
c++ 백준_1167_"트리의 지름"  (2) 2023.06.13
c++ 백준_1918_"후위 표기식"  (2) 2023.06.13
c++ 백준_1132_"합"  (0) 2023.06.13