Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c882e5e

Browse filesBrowse files
authored
Merge Seunghwan boj 1715 카드정렬하기
Seunghwan boj 1715 카드정렬하기
2 parents 0d24d8e + e34cc85 commit c882e5e
Copy full SHA for c882e5e

File tree

Expand file treeCollapse file tree

2 files changed

+89
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+89
-0
lines changed
Open diff view settings
Collapse file
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Gold;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.util.PriorityQueue;
6+
7+
public class G4_1715카드정렬하기 {
8+
9+
public static void main(String[] args) throws Exception {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
12+
int N = Integer.parseInt(br.readLine());
13+
14+
for (int i = 0; i < N; i++)
15+
pq.add(Integer.parseInt(br.readLine()));
16+
int answer = 0;
17+
while (pq.size() != 1) {
18+
int cnt = pq.poll() + pq.poll();
19+
pq.add(cnt);
20+
answer += cnt;
21+
}
22+
System.out.println(answer);
23+
}
24+
25+
}
Collapse file

‎seunghwan/boj/1715/README.md‎

Copy file name to clipboard
+64Lines changed: 64 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 백준 1715 카드정렬하기
2+
3+
[문제 링크](https://www.acmicpc.net/problem/1715)
4+
5+
## 1. 설계 로직
6+
7+
현재 가지고 있는 카드뭉치 중 가장 작은 것 2개를 뽑아 새 뭉치를 만들면 최소의 비교로 가능하다.
8+
9+
10+
11+
여기서 현재 가지고 있는 카드뭉치는 새롭게 합쳐져 만들어진 뭉치까지 포함해서 작은 것 2개이다.
12+
13+
14+
15+
1. pq에 모든 카드 갯수 넣기
16+
17+
2. 작은 것 2개를 뽑아 비교횟수를 answer에 저장하고
18+
19+
3. 새로 만들어진 뭉치를 pq에 다시 넣기
20+
21+
4. 뭉치가 하나만 남을 때까지 돌고 answer 출력
22+
23+
24+
25+
- 시간복잡도
26+
27+
O(N)
28+
29+
## 2. 코드
30+
31+
```java
32+
package Gold;
33+
34+
import java.io.BufferedReader;
35+
import java.io.InputStreamReader;
36+
import java.util.PriorityQueue;
37+
38+
public class G4_1715카드정렬하기 {
39+
40+
public static void main(String[] args) throws Exception {
41+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
42+
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
43+
int N = Integer.parseInt(br.readLine());
44+
45+
for (int i = 0; i < N; i++)
46+
pq.add(Integer.parseInt(br.readLine()));
47+
int answer = 0;
48+
while (pq.size() != 1) {
49+
int cnt = pq.poll() + pq.poll();
50+
pq.add(cnt);
51+
answer += cnt;
52+
}
53+
System.out.println(answer);
54+
}
55+
56+
}
57+
```
58+
59+
60+
61+
## 3. 후기
62+
63+
- 많이 겪어본 유형이라 어렵지 않게 해결 가능했다.
64+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.