일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- graphics
- ComputeShader
- Algorithm
- 학습용
- 알고리즘
- game jam
- rendering pipeline
- C
- 알고리즘연습
- 2020.03.16
- dedicatedserver
- 독서
- TIP
- ue5
- class
- UNORDERED_MAP
- 참조자
- sparse matrix
- 2020.02.23
- Overloading
- Conjugate Gradient
- C++
- 논문
- Implicit method
- ppt
- Til
- listenserver
- stretch force
- 프로그래머스
- 백준
Archives
- Today
- Total
OSgood의 개발일기
[백준] 1715번 카드 정렬하기 본문
전형적인 priority_queue(minheap)를 이용하는 문제이다.
https://www.acmicpc.net/problem/1715
1715번: 카드 정렬하기
정렬된 두 묶음의 숫자 카드가 있다고 하자. 각 묶음의 카드의 수를 A, B라 하면 보통 두 묶음을 합쳐서 하나로 만드는 데에는 A+B 번의 비교를 해야 한다. 이를테면, 20장의 숫자 카드 묶음과 30장의 숫자 카드 묶음을 합치려면 50번의 비교가 필요하다. 매우 많은 숫자 카드 묶음이 책상 위에 놓여 있다. 이들을 두 묶음씩 골라 서로 합쳐나간다면, 고르는 순서에 따라서 비교 횟수가 매우 달라진다. 예를 들어 10장, 20장, 40장의 묶음이 있다면
www.acmicpc.net
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <iostream> #include <queue> using namespace std; int main() { int N = 0; priority_queue<int> cards; cin >> N; for (int i = 0; i < N; i++) { int temp; cin >> temp; cards.push(-temp); } int result = 0; if (cards.size() == 1) { cout << 0 << endl; return 0; } while (!cards.empty() ) { int x = cards.top(); cards.pop(); int y = cards.top(); cards.pop(); result += (x + y); if(cards.empty()) break; cards.push((x + y)); } cout << -result << endl; return 0; } | cs |
'Algorithm > Algorithm 문제 연습' 카테고리의 다른 글
[백준] 10166번 관중석 (0) | 2020.03.13 |
---|---|
[백준] 10837번 동전 게임 (0) | 2020.03.13 |
[백준] 2583번 영역 구하기 (0) | 2020.02.23 |
[백준] 14867번 물통문제 (0) | 2020.02.15 |
[CodeUp]2016 천단위 구분기호 (0) | 2019.09.10 |
Comments