Skip to content

Commit 0fd496a

Browse files
authored
Merge pull request #208 from JavaCote/baekgwa
강성욱 37주차
2 parents bae539b + c5786e8 commit 0fd496a

File tree

4 files changed

+342
-0
lines changed

4 files changed

+342
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package week37.강성욱.baekjoon;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.Queue;
10+
import java.util.StringTokenizer;
11+
12+
/**
13+
* PackageName : week37.강성욱.baekjoon
14+
* FileName : DFS와_BFS
15+
* Author : Baekgwa
16+
* Date : 26. 1. 12.
17+
* Description :
18+
* =====================================================================================================================
19+
* DATE AUTHOR NOTE
20+
* ---------------------------------------------------------------------------------------------------------------------
21+
* 26. 1. 12. Baekgwa Initial creation
22+
*/
23+
public class DFS와_BFS {
24+
public class Main {
25+
26+
private static List<Integer> dfsResult = new ArrayList<>();
27+
private static boolean[] dfsVisited;
28+
29+
private static List<Integer> bfsResult = new ArrayList<>();
30+
private static boolean[] bfsVisited;
31+
32+
public static void main(String[] args) throws Exception {
33+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
34+
StringTokenizer st = new StringTokenizer(br.readLine());
35+
36+
int N = Integer.parseInt(st.nextToken());
37+
int M = Integer.parseInt(st.nextToken());
38+
int V = Integer.parseInt(st.nextToken()); // 시작 정점
39+
40+
// List<PriorityQueue<Integer>> graph = new ArrayList<>();
41+
// for(int i=0; i<=N; i++) graph.add(new PriorityQueue<>()); // 방문할 곳 많으면, 작은순서로 해야해서 그냥 PQ 쓰기
42+
List<List<Integer>> graph = new ArrayList<>();
43+
for(int i=0; i<=N; i++) graph.add(new ArrayList<>());
44+
45+
for(int i=0; i<M; i++) {
46+
st = new StringTokenizer(br.readLine());
47+
int A = Integer.parseInt(st.nextToken());
48+
int B = Integer.parseInt(st.nextToken());
49+
50+
graph.get(A).add(B);
51+
graph.get(B).add(A);
52+
}
53+
54+
for(int i=0; i<=N; i++) {
55+
Collections.sort(graph.get(i));
56+
}
57+
58+
dfsVisited = new boolean[N+1];
59+
bfsVisited = new boolean[N+1];
60+
61+
dfs(graph, V);
62+
bfs(graph, V);
63+
64+
System.out.println(toString(dfsResult));
65+
System.out.println(toString(bfsResult));
66+
}
67+
68+
private static String toString(List<Integer> list) {
69+
StringBuilder sb = new StringBuilder();
70+
71+
for (int now : list) {
72+
sb.append(now).append(" ");
73+
}
74+
75+
sb.deleteCharAt(sb.length()-1);
76+
77+
return sb.toString();
78+
}
79+
80+
private static void dfs(List<List<Integer>> graph, int now) {
81+
dfsResult.add(now);
82+
dfsVisited[now] = true;
83+
84+
for (int nextNode : graph.get(now)) {
85+
if(dfsVisited[nextNode]) continue;
86+
dfs(graph, nextNode);
87+
}
88+
}
89+
90+
private static void bfs(List<List<Integer>> graph, int now) {
91+
Queue<Integer> q = new LinkedList<>();
92+
bfsVisited[now] = true;
93+
q.offer(now);
94+
95+
while (!q.isEmpty()) {
96+
int nowNode = q.poll();
97+
bfsResult.add(nowNode);
98+
99+
for(int nextNode : graph.get(nowNode)) {
100+
if(bfsVisited[nextNode]) continue;
101+
102+
bfsVisited[nextNode] = true;
103+
q.offer(nextNode);
104+
}
105+
}
106+
}
107+
}
108+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package week37.강성욱.baekjoon;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.util.ArrayList;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Queue;
9+
import java.util.StringTokenizer;
10+
11+
/**
12+
* PackageName : week37.강성욱.baekjoon
13+
* FileName : 줄_세우기
14+
* Author : Baekgwa
15+
* Date : 26. 1. 12.
16+
* Description :
17+
* =====================================================================================================================
18+
* DATE AUTHOR NOTE
19+
* ---------------------------------------------------------------------------------------------------------------------
20+
* 26. 1. 12. Baekgwa Initial creation
21+
*/
22+
public class 줄_세우기 {
23+
public class Main {
24+
public static void main(String[] args) throws Exception {
25+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
26+
StringTokenizer st = new StringTokenizer(br.readLine());
27+
int N = Integer.parseInt(st.nextToken()); // 학생 수
28+
int M = Integer.parseInt(st.nextToken()); // 비교 횟수
29+
30+
List<List<Integer>> effectList = new ArrayList<>(); //각 정점이 어떤 정점들에게 영향을 끼쳤는지 확인하기 위한 리스트
31+
for (int i = 0; i <= N; i++) {
32+
effectList.add(new ArrayList<>());
33+
}
34+
35+
int[] ingress = new int[N + 1]; //각 정점이 result 에 저장되기 전에, 먼저 처리되어야 하는 정점의 개수
36+
37+
for (int i = 0; i < M; i++) {
38+
st = new StringTokenizer(br.readLine());
39+
// 학생 A가 B 앞에 있어야함.
40+
int A = Integer.parseInt(st.nextToken());
41+
int B = Integer.parseInt(st.nextToken());
42+
43+
ingress[B]++; // B 정점은, 누군진 몰라도, 앞에 정점 하나가 먼저 처리되어야 등장이 가능
44+
effectList.get(A).add(B); // A 정점은, B 정점에게 영향을 끼쳤다.
45+
}
46+
47+
// ingress[Node] 의 값이 0인 친구들을 넣을 곳
48+
// 즉, q에는 현재 언제 나와도 상관없는 정점들의 큐
49+
Queue<Integer> q = new LinkedList<>();
50+
insertZeroIngressNode(ingress, q);
51+
52+
StringBuilder sb= new StringBuilder();
53+
54+
while(!q.isEmpty()) {
55+
Integer nowNode = q.poll();
56+
sb.append(nowNode).append(" ");
57+
58+
for (int effectedNode : effectList.get(nowNode)) {
59+
ingress[effectedNode]--;
60+
61+
if(ingress[effectedNode] == 0) {
62+
q.add(effectedNode);
63+
}
64+
}
65+
}
66+
67+
System.out.println(sb.toString());
68+
}
69+
70+
private static void insertZeroIngressNode(int[] ingress, Queue<Integer> q) {
71+
for(int i=1; i<ingress.length; i++){
72+
if(ingress[i] == 0) {
73+
q.add(i);
74+
ingress[i]--;
75+
}
76+
}
77+
}
78+
}
79+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package week37.강성욱.baekjoon;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.util.StringTokenizer;
6+
7+
/**
8+
* PackageName : week37.강성욱.baekjoon
9+
* FileName : 터렛
10+
* Author : Baekgwa
11+
* Date : 26. 1. 12.
12+
* Description :
13+
* =====================================================================================================================
14+
* DATE AUTHOR NOTE
15+
* ---------------------------------------------------------------------------------------------------------------------
16+
* 26. 1. 12. Baekgwa Initial creation
17+
*/
18+
public class 터렛 {
19+
public class Main {
20+
// 좌표 기준으로, 거리는 반지름.
21+
// 결국 교집합을 구하라는 거잖아.
22+
public static void main(String[] args) throws Exception {
23+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
24+
StringBuilder sb = new StringBuilder();
25+
26+
int T = Integer.parseInt(br.readLine());
27+
28+
while (T-- > 0) {
29+
StringTokenizer st = new StringTokenizer(br.readLine());
30+
31+
int x1 = Integer.parseInt(st.nextToken());
32+
int y1 = Integer.parseInt(st.nextToken());
33+
int r1 = Integer.parseInt(st.nextToken());
34+
35+
int x2 = Integer.parseInt(st.nextToken());
36+
int y2 = Integer.parseInt(st.nextToken());
37+
int r2 = Integer.parseInt(st.nextToken());
38+
39+
sb.append(countIntersection(x1, y1, r1, x2, y2, r2)).append('\n');
40+
}
41+
42+
System.out.print(sb);
43+
}
44+
45+
private static int countIntersection(
46+
int x1, int y1, int r1,
47+
int x2, int y2, int r2
48+
) {
49+
long dx = x1 - x2;
50+
long dy = y1 - y2;
51+
52+
long distSq = dx * dx + dy * dy;
53+
long sumR = r1 + r2;
54+
long diffR = Math.abs(r1 - r2);
55+
56+
long sumRSq = sumR * sumR;
57+
long diffRSq = diffR * diffR;
58+
59+
// 중심이 같고 반지름도 같음 → 무한대
60+
if (distSq == 0 && r1 == r2) {
61+
return -1;
62+
}
63+
64+
// 교점 없음
65+
if (distSq > sumRSq) {
66+
return 0;
67+
}
68+
if (distSq < diffRSq) {
69+
return 0;
70+
}
71+
72+
// 한 점에서 만남
73+
if (distSq == sumRSq) {
74+
return 1;
75+
}
76+
if (distSq == diffRSq) {
77+
return 1;
78+
}
79+
80+
// 두 점에서 만남
81+
return 2;
82+
}
83+
}
84+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package week37.강성욱.baekjoon;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.util.PriorityQueue;
6+
import java.util.StringTokenizer;
7+
8+
/**
9+
* PackageName : week37.강성욱.baekjoon
10+
* FileName : 파일_합치기3
11+
* Author : Baekgwa
12+
* Date : 26. 1. 12.
13+
* Description :
14+
* =====================================================================================================================
15+
* DATE AUTHOR NOTE
16+
* ---------------------------------------------------------------------------------------------------------------------
17+
* 26. 1. 12. Baekgwa Initial creation
18+
*/
19+
public class 파일_합치기3 {
20+
public class Main {
21+
22+
// 항상 최소한의 합과 합으로 구해나가면 되는거 아냐?
23+
// 1, 1, 1, 100, 200
24+
// 2 1 100 200 /비용:2
25+
// 3 100 200 / 비용 2+3
26+
// 103 200 / 비용 2 + 3 + 103
27+
// ...
28+
29+
// 아 40, 45, 55, 60
30+
// 이런케이스에는 좀 문제가 되겠네
31+
// 85, 55, 60 / 비용 : 85
32+
// 여기서, 55랑 60 을 더하는게 낫네
33+
// 85, 115 / 비용 : 85 + 115
34+
// 200 / 비용 : 85 + 115 + 200
35+
36+
// 만약 저기서, 그대로 앞에서 부터 진행했으면
37+
// 140, 60 / 비용 : 85 + 140
38+
// 200 / 비용 85 + 140 + 200
39+
// 아 이렇게 풀면 안되네... 순서는 정해진대로 들어가야 하구나
40+
public static void main(String[] args) throws Exception {
41+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
42+
int testCase = Integer.parseInt(br.readLine());
43+
44+
StringBuilder result = new StringBuilder();
45+
46+
while(testCase-- > 0) {
47+
int K = Integer.parseInt(br.readLine()); // 소설의 장 수
48+
49+
PriorityQueue<Long> pq = new PriorityQueue<>();
50+
StringTokenizer st = new StringTokenizer(br.readLine());
51+
for(int i=0; i<K; i++) {
52+
pq.offer(Long.parseLong(st.nextToken()));
53+
}
54+
55+
long cost = 0;
56+
while(pq.size() >= 2) {
57+
long left = pq.poll();
58+
long right = pq.poll();
59+
60+
long nowSum = left + right;
61+
cost += nowSum;
62+
pq.add(nowSum);
63+
}
64+
65+
result.append(cost).append("\n");
66+
}
67+
68+
System.out.println(result.toString());
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)